Jason Sams | eb4fe18 | 2011-05-26 16:33:01 -0700 | [diff] [blame] | 1 | /* |
Jason Sams | bc0ca6b | 2013-02-15 18:13:43 -0800 | [diff] [blame] | 2 | * Copyright (C) 2013 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 | |
Jason Sams | eb4fe18 | 2011-05-26 16:33:01 -0700 | [diff] [blame] | 17 | #include "rsdCore.h" |
Jason Sams | eb4fe18 | 2011-05-26 16:33:01 -0700 | [diff] [blame] | 18 | #include "rsdAllocation.h" |
| 19 | |
| 20 | #include "rsAllocation.h" |
| 21 | |
Stephen Hines | b0934b6 | 2013-07-03 17:27:38 -0700 | [diff] [blame] | 22 | #if !defined(RS_SERVER) && !defined(RS_COMPATIBILITY_LIB) |
Jason Sams | 7ac2a4d | 2012-02-15 12:04:24 -0800 | [diff] [blame] | 23 | #include "system/window.h" |
Jason Sams | 7ac2a4d | 2012-02-15 12:04:24 -0800 | [diff] [blame] | 24 | #include "ui/Rect.h" |
| 25 | #include "ui/GraphicBufferMapper.h" |
Stephen Hines | b0934b6 | 2013-07-03 17:27:38 -0700 | [diff] [blame] | 26 | #else |
| 27 | #include "system/graphics.h" |
Tim Murray | 0b575de | 2013-03-15 15:56:43 -0700 | [diff] [blame] | 28 | #endif |
Jason Sams | 93eacc7 | 2012-12-18 14:26:57 -0800 | [diff] [blame] | 29 | |
| 30 | #ifndef RS_COMPATIBILITY_LIB |
| 31 | #include "rsdFrameBufferObj.h" |
Andy McFadden | 58fd6a5 | 2012-12-18 09:50:03 -0800 | [diff] [blame] | 32 | #include "gui/GLConsumer.h" |
Jason Sams | 733396b | 2013-02-22 12:46:18 -0800 | [diff] [blame] | 33 | #include "gui/CpuConsumer.h" |
| 34 | #include "gui/Surface.h" |
Jason Sams | 93eacc7 | 2012-12-18 14:26:57 -0800 | [diff] [blame] | 35 | #include "hardware/gralloc.h" |
Jason Sams | 7ac2a4d | 2012-02-15 12:04:24 -0800 | [diff] [blame] | 36 | |
Jason Sams | eb4fe18 | 2011-05-26 16:33:01 -0700 | [diff] [blame] | 37 | #include <GLES/gl.h> |
| 38 | #include <GLES2/gl2.h> |
| 39 | #include <GLES/glext.h> |
Jason Sams | 93eacc7 | 2012-12-18 14:26:57 -0800 | [diff] [blame] | 40 | #endif |
Jason Sams | eb4fe18 | 2011-05-26 16:33:01 -0700 | [diff] [blame] | 41 | |
Tim Murray | 0b575de | 2013-03-15 15:56:43 -0700 | [diff] [blame] | 42 | #ifdef RS_SERVER |
| 43 | // server requires malloc.h for memalign |
| 44 | #include <malloc.h> |
| 45 | #endif |
| 46 | |
Jason Sams | eb4fe18 | 2011-05-26 16:33:01 -0700 | [diff] [blame] | 47 | using namespace android; |
| 48 | using namespace android::renderscript; |
| 49 | |
| 50 | |
Jason Sams | 93eacc7 | 2012-12-18 14:26:57 -0800 | [diff] [blame] | 51 | #ifndef RS_COMPATIBILITY_LIB |
Jason Sams | eb4fe18 | 2011-05-26 16:33:01 -0700 | [diff] [blame] | 52 | const static GLenum gFaceOrder[] = { |
| 53 | GL_TEXTURE_CUBE_MAP_POSITIVE_X, |
| 54 | GL_TEXTURE_CUBE_MAP_NEGATIVE_X, |
| 55 | GL_TEXTURE_CUBE_MAP_POSITIVE_Y, |
| 56 | GL_TEXTURE_CUBE_MAP_NEGATIVE_Y, |
| 57 | GL_TEXTURE_CUBE_MAP_POSITIVE_Z, |
| 58 | GL_TEXTURE_CUBE_MAP_NEGATIVE_Z |
| 59 | }; |
| 60 | |
Jason Sams | a614ae1 | 2011-05-26 17:05:51 -0700 | [diff] [blame] | 61 | GLenum rsdTypeToGLType(RsDataType t) { |
| 62 | switch (t) { |
| 63 | case RS_TYPE_UNSIGNED_5_6_5: return GL_UNSIGNED_SHORT_5_6_5; |
| 64 | case RS_TYPE_UNSIGNED_5_5_5_1: return GL_UNSIGNED_SHORT_5_5_5_1; |
| 65 | case RS_TYPE_UNSIGNED_4_4_4_4: return GL_UNSIGNED_SHORT_4_4_4_4; |
| 66 | |
| 67 | //case RS_TYPE_FLOAT_16: return GL_HALF_FLOAT; |
| 68 | case RS_TYPE_FLOAT_32: return GL_FLOAT; |
| 69 | case RS_TYPE_UNSIGNED_8: return GL_UNSIGNED_BYTE; |
| 70 | case RS_TYPE_UNSIGNED_16: return GL_UNSIGNED_SHORT; |
| 71 | case RS_TYPE_SIGNED_8: return GL_BYTE; |
| 72 | case RS_TYPE_SIGNED_16: return GL_SHORT; |
| 73 | default: break; |
| 74 | } |
| 75 | return 0; |
| 76 | } |
| 77 | |
| 78 | GLenum rsdKindToGLFormat(RsDataKind k) { |
| 79 | switch (k) { |
| 80 | case RS_KIND_PIXEL_L: return GL_LUMINANCE; |
| 81 | case RS_KIND_PIXEL_A: return GL_ALPHA; |
| 82 | case RS_KIND_PIXEL_LA: return GL_LUMINANCE_ALPHA; |
| 83 | case RS_KIND_PIXEL_RGB: return GL_RGB; |
| 84 | case RS_KIND_PIXEL_RGBA: return GL_RGBA; |
| 85 | case RS_KIND_PIXEL_DEPTH: return GL_DEPTH_COMPONENT16; |
| 86 | default: break; |
| 87 | } |
| 88 | return 0; |
| 89 | } |
Jason Sams | 93eacc7 | 2012-12-18 14:26:57 -0800 | [diff] [blame] | 90 | #endif |
Jason Sams | a614ae1 | 2011-05-26 17:05:51 -0700 | [diff] [blame] | 91 | |
Jason Sams | 807fdc4 | 2012-07-25 17:55:39 -0700 | [diff] [blame] | 92 | uint8_t *GetOffsetPtr(const android::renderscript::Allocation *alloc, |
Jason Sams | 3bbc0fd | 2013-04-09 14:16:13 -0700 | [diff] [blame] | 93 | uint32_t xoff, uint32_t yoff, uint32_t zoff, |
| 94 | uint32_t lod, RsAllocationCubemapFace face) { |
Jason Sams | 709a097 | 2012-11-15 18:18:04 -0800 | [diff] [blame] | 95 | uint8_t *ptr = (uint8_t *)alloc->mHal.drvState.lod[lod].mallocPtr; |
| 96 | ptr += face * alloc->mHal.drvState.faceOffset; |
Jason Sams | 3bbc0fd | 2013-04-09 14:16:13 -0700 | [diff] [blame] | 97 | ptr += zoff * alloc->mHal.drvState.lod[lod].dimY * alloc->mHal.drvState.lod[lod].stride; |
Jason Sams | 709a097 | 2012-11-15 18:18:04 -0800 | [diff] [blame] | 98 | ptr += yoff * alloc->mHal.drvState.lod[lod].stride; |
Jason Sams | 807fdc4 | 2012-07-25 17:55:39 -0700 | [diff] [blame] | 99 | ptr += xoff * alloc->mHal.state.elementSizeBytes; |
| 100 | return ptr; |
| 101 | } |
| 102 | |
Jason Sams | a614ae1 | 2011-05-26 17:05:51 -0700 | [diff] [blame] | 103 | |
Jason Sams | 2382aba | 2011-09-13 15:41:01 -0700 | [diff] [blame] | 104 | static void Update2DTexture(const Context *rsc, const Allocation *alloc, const void *ptr, |
| 105 | uint32_t xoff, uint32_t yoff, uint32_t lod, |
| 106 | RsAllocationCubemapFace face, uint32_t w, uint32_t h) { |
Jason Sams | 93eacc7 | 2012-12-18 14:26:57 -0800 | [diff] [blame] | 107 | #ifndef RS_COMPATIBILITY_LIB |
Jason Sams | eb4fe18 | 2011-05-26 16:33:01 -0700 | [diff] [blame] | 108 | DrvAllocation *drv = (DrvAllocation *)alloc->mHal.drv; |
| 109 | |
Jason Sams | eb4fe18 | 2011-05-26 16:33:01 -0700 | [diff] [blame] | 110 | rsAssert(drv->textureID); |
Jason Sams | 2382aba | 2011-09-13 15:41:01 -0700 | [diff] [blame] | 111 | RSD_CALL_GL(glBindTexture, drv->glTarget, drv->textureID); |
| 112 | RSD_CALL_GL(glPixelStorei, GL_UNPACK_ALIGNMENT, 1); |
Jason Sams | eb4fe18 | 2011-05-26 16:33:01 -0700 | [diff] [blame] | 113 | GLenum t = GL_TEXTURE_2D; |
| 114 | if (alloc->mHal.state.hasFaces) { |
| 115 | t = gFaceOrder[face]; |
| 116 | } |
Jason Sams | 2382aba | 2011-09-13 15:41:01 -0700 | [diff] [blame] | 117 | RSD_CALL_GL(glTexSubImage2D, t, lod, xoff, yoff, w, h, drv->glFormat, drv->glType, ptr); |
Jason Sams | 93eacc7 | 2012-12-18 14:26:57 -0800 | [diff] [blame] | 118 | #endif |
Jason Sams | eb4fe18 | 2011-05-26 16:33:01 -0700 | [diff] [blame] | 119 | } |
| 120 | |
| 121 | |
Jason Sams | 93eacc7 | 2012-12-18 14:26:57 -0800 | [diff] [blame] | 122 | #ifndef RS_COMPATIBILITY_LIB |
Jason Sams | eb4fe18 | 2011-05-26 16:33:01 -0700 | [diff] [blame] | 123 | static void Upload2DTexture(const Context *rsc, const Allocation *alloc, bool isFirstUpload) { |
| 124 | DrvAllocation *drv = (DrvAllocation *)alloc->mHal.drv; |
| 125 | |
Jason Sams | 2382aba | 2011-09-13 15:41:01 -0700 | [diff] [blame] | 126 | RSD_CALL_GL(glBindTexture, drv->glTarget, drv->textureID); |
| 127 | RSD_CALL_GL(glPixelStorei, GL_UNPACK_ALIGNMENT, 1); |
Jason Sams | eb4fe18 | 2011-05-26 16:33:01 -0700 | [diff] [blame] | 128 | |
| 129 | uint32_t faceCount = 1; |
| 130 | if (alloc->mHal.state.hasFaces) { |
| 131 | faceCount = 6; |
| 132 | } |
| 133 | |
| 134 | rsdGLCheckError(rsc, "Upload2DTexture 1 "); |
| 135 | for (uint32_t face = 0; face < faceCount; face ++) { |
| 136 | for (uint32_t lod = 0; lod < alloc->mHal.state.type->getLODCount(); lod++) { |
Jason Sams | 3bbc0fd | 2013-04-09 14:16:13 -0700 | [diff] [blame] | 137 | const uint8_t *p = GetOffsetPtr(alloc, 0, 0, 0, lod, (RsAllocationCubemapFace)face); |
Jason Sams | eb4fe18 | 2011-05-26 16:33:01 -0700 | [diff] [blame] | 138 | |
| 139 | GLenum t = GL_TEXTURE_2D; |
| 140 | if (alloc->mHal.state.hasFaces) { |
| 141 | t = gFaceOrder[face]; |
| 142 | } |
| 143 | |
| 144 | if (isFirstUpload) { |
Jason Sams | 2382aba | 2011-09-13 15:41:01 -0700 | [diff] [blame] | 145 | RSD_CALL_GL(glTexImage2D, t, lod, drv->glFormat, |
Jason Sams | eb4fe18 | 2011-05-26 16:33:01 -0700 | [diff] [blame] | 146 | alloc->mHal.state.type->getLODDimX(lod), |
| 147 | alloc->mHal.state.type->getLODDimY(lod), |
Jason Sams | a614ae1 | 2011-05-26 17:05:51 -0700 | [diff] [blame] | 148 | 0, drv->glFormat, drv->glType, p); |
Jason Sams | eb4fe18 | 2011-05-26 16:33:01 -0700 | [diff] [blame] | 149 | } else { |
Jason Sams | 2382aba | 2011-09-13 15:41:01 -0700 | [diff] [blame] | 150 | RSD_CALL_GL(glTexSubImage2D, t, lod, 0, 0, |
Jason Sams | eb4fe18 | 2011-05-26 16:33:01 -0700 | [diff] [blame] | 151 | alloc->mHal.state.type->getLODDimX(lod), |
| 152 | alloc->mHal.state.type->getLODDimY(lod), |
Jason Sams | a614ae1 | 2011-05-26 17:05:51 -0700 | [diff] [blame] | 153 | drv->glFormat, drv->glType, p); |
Jason Sams | eb4fe18 | 2011-05-26 16:33:01 -0700 | [diff] [blame] | 154 | } |
| 155 | } |
| 156 | } |
| 157 | |
| 158 | if (alloc->mHal.state.mipmapControl == RS_ALLOCATION_MIPMAP_ON_SYNC_TO_TEXTURE) { |
Jason Sams | 2382aba | 2011-09-13 15:41:01 -0700 | [diff] [blame] | 159 | RSD_CALL_GL(glGenerateMipmap, drv->glTarget); |
Jason Sams | eb4fe18 | 2011-05-26 16:33:01 -0700 | [diff] [blame] | 160 | } |
| 161 | rsdGLCheckError(rsc, "Upload2DTexture"); |
| 162 | } |
Jason Sams | 93eacc7 | 2012-12-18 14:26:57 -0800 | [diff] [blame] | 163 | #endif |
Jason Sams | eb4fe18 | 2011-05-26 16:33:01 -0700 | [diff] [blame] | 164 | |
| 165 | static void UploadToTexture(const Context *rsc, const Allocation *alloc) { |
Jason Sams | 93eacc7 | 2012-12-18 14:26:57 -0800 | [diff] [blame] | 166 | #ifndef RS_COMPATIBILITY_LIB |
Jason Sams | eb4fe18 | 2011-05-26 16:33:01 -0700 | [diff] [blame] | 167 | DrvAllocation *drv = (DrvAllocation *)alloc->mHal.drv; |
| 168 | |
Jason Sams | 3522f40 | 2012-03-23 11:47:26 -0700 | [diff] [blame] | 169 | if (alloc->mHal.state.usageFlags & RS_ALLOCATION_USAGE_IO_INPUT) { |
Jason Sams | 41e373d | 2012-01-13 14:01:20 -0800 | [diff] [blame] | 170 | if (!drv->textureID) { |
| 171 | RSD_CALL_GL(glGenTextures, 1, &drv->textureID); |
| 172 | } |
| 173 | return; |
| 174 | } |
| 175 | |
Jason Sams | a614ae1 | 2011-05-26 17:05:51 -0700 | [diff] [blame] | 176 | if (!drv->glType || !drv->glFormat) { |
Jason Sams | eb4fe18 | 2011-05-26 16:33:01 -0700 | [diff] [blame] | 177 | return; |
| 178 | } |
| 179 | |
Jason Sams | 709a097 | 2012-11-15 18:18:04 -0800 | [diff] [blame] | 180 | if (!alloc->mHal.drvState.lod[0].mallocPtr) { |
Jason Sams | eb4fe18 | 2011-05-26 16:33:01 -0700 | [diff] [blame] | 181 | return; |
| 182 | } |
| 183 | |
| 184 | bool isFirstUpload = false; |
| 185 | |
| 186 | if (!drv->textureID) { |
Jason Sams | 2382aba | 2011-09-13 15:41:01 -0700 | [diff] [blame] | 187 | RSD_CALL_GL(glGenTextures, 1, &drv->textureID); |
Jason Sams | eb4fe18 | 2011-05-26 16:33:01 -0700 | [diff] [blame] | 188 | isFirstUpload = true; |
| 189 | } |
| 190 | |
| 191 | Upload2DTexture(rsc, alloc, isFirstUpload); |
| 192 | |
| 193 | if (!(alloc->mHal.state.usageFlags & RS_ALLOCATION_USAGE_SCRIPT)) { |
Jason Sams | 709a097 | 2012-11-15 18:18:04 -0800 | [diff] [blame] | 194 | if (alloc->mHal.drvState.lod[0].mallocPtr) { |
| 195 | free(alloc->mHal.drvState.lod[0].mallocPtr); |
| 196 | alloc->mHal.drvState.lod[0].mallocPtr = NULL; |
Jason Sams | eb4fe18 | 2011-05-26 16:33:01 -0700 | [diff] [blame] | 197 | } |
| 198 | } |
| 199 | rsdGLCheckError(rsc, "UploadToTexture"); |
Jason Sams | 93eacc7 | 2012-12-18 14:26:57 -0800 | [diff] [blame] | 200 | #endif |
Jason Sams | eb4fe18 | 2011-05-26 16:33:01 -0700 | [diff] [blame] | 201 | } |
| 202 | |
| 203 | static void AllocateRenderTarget(const Context *rsc, const Allocation *alloc) { |
Jason Sams | 93eacc7 | 2012-12-18 14:26:57 -0800 | [diff] [blame] | 204 | #ifndef RS_COMPATIBILITY_LIB |
Jason Sams | eb4fe18 | 2011-05-26 16:33:01 -0700 | [diff] [blame] | 205 | DrvAllocation *drv = (DrvAllocation *)alloc->mHal.drv; |
| 206 | |
Jason Sams | a614ae1 | 2011-05-26 17:05:51 -0700 | [diff] [blame] | 207 | if (!drv->glFormat) { |
Jason Sams | eb4fe18 | 2011-05-26 16:33:01 -0700 | [diff] [blame] | 208 | return; |
| 209 | } |
| 210 | |
| 211 | if (!drv->renderTargetID) { |
Jason Sams | 2382aba | 2011-09-13 15:41:01 -0700 | [diff] [blame] | 212 | RSD_CALL_GL(glGenRenderbuffers, 1, &drv->renderTargetID); |
Jason Sams | eb4fe18 | 2011-05-26 16:33:01 -0700 | [diff] [blame] | 213 | |
| 214 | if (!drv->renderTargetID) { |
| 215 | // This should generally not happen |
Steve Block | af12ac6 | 2012-01-06 19:20:56 +0000 | [diff] [blame] | 216 | ALOGE("allocateRenderTarget failed to gen mRenderTargetID"); |
Jason Sams | eb4fe18 | 2011-05-26 16:33:01 -0700 | [diff] [blame] | 217 | rsc->dumpDebug(); |
| 218 | return; |
| 219 | } |
Jason Sams | 2382aba | 2011-09-13 15:41:01 -0700 | [diff] [blame] | 220 | RSD_CALL_GL(glBindRenderbuffer, GL_RENDERBUFFER, drv->renderTargetID); |
| 221 | RSD_CALL_GL(glRenderbufferStorage, GL_RENDERBUFFER, drv->glFormat, |
Jason Sams | a572aca | 2013-01-09 11:52:26 -0800 | [diff] [blame] | 222 | alloc->mHal.drvState.lod[0].dimX, alloc->mHal.drvState.lod[0].dimY); |
Jason Sams | eb4fe18 | 2011-05-26 16:33:01 -0700 | [diff] [blame] | 223 | } |
| 224 | rsdGLCheckError(rsc, "AllocateRenderTarget"); |
Jason Sams | 93eacc7 | 2012-12-18 14:26:57 -0800 | [diff] [blame] | 225 | #endif |
Jason Sams | eb4fe18 | 2011-05-26 16:33:01 -0700 | [diff] [blame] | 226 | } |
| 227 | |
| 228 | static void UploadToBufferObject(const Context *rsc, const Allocation *alloc) { |
Jason Sams | 93eacc7 | 2012-12-18 14:26:57 -0800 | [diff] [blame] | 229 | #ifndef RS_COMPATIBILITY_LIB |
Jason Sams | eb4fe18 | 2011-05-26 16:33:01 -0700 | [diff] [blame] | 230 | DrvAllocation *drv = (DrvAllocation *)alloc->mHal.drv; |
| 231 | |
| 232 | rsAssert(!alloc->mHal.state.type->getDimY()); |
| 233 | rsAssert(!alloc->mHal.state.type->getDimZ()); |
| 234 | |
| 235 | //alloc->mHal.state.usageFlags |= RS_ALLOCATION_USAGE_GRAPHICS_VERTEX; |
| 236 | |
| 237 | if (!drv->bufferID) { |
Jason Sams | 2382aba | 2011-09-13 15:41:01 -0700 | [diff] [blame] | 238 | RSD_CALL_GL(glGenBuffers, 1, &drv->bufferID); |
Jason Sams | eb4fe18 | 2011-05-26 16:33:01 -0700 | [diff] [blame] | 239 | } |
| 240 | if (!drv->bufferID) { |
Steve Block | af12ac6 | 2012-01-06 19:20:56 +0000 | [diff] [blame] | 241 | ALOGE("Upload to buffer object failed"); |
Jason Sams | eb4fe18 | 2011-05-26 16:33:01 -0700 | [diff] [blame] | 242 | drv->uploadDeferred = true; |
| 243 | return; |
| 244 | } |
Jason Sams | 2382aba | 2011-09-13 15:41:01 -0700 | [diff] [blame] | 245 | RSD_CALL_GL(glBindBuffer, drv->glTarget, drv->bufferID); |
| 246 | RSD_CALL_GL(glBufferData, drv->glTarget, alloc->mHal.state.type->getSizeBytes(), |
Jason Sams | 709a097 | 2012-11-15 18:18:04 -0800 | [diff] [blame] | 247 | alloc->mHal.drvState.lod[0].mallocPtr, GL_DYNAMIC_DRAW); |
Jason Sams | 2382aba | 2011-09-13 15:41:01 -0700 | [diff] [blame] | 248 | RSD_CALL_GL(glBindBuffer, drv->glTarget, 0); |
Jason Sams | eb4fe18 | 2011-05-26 16:33:01 -0700 | [diff] [blame] | 249 | rsdGLCheckError(rsc, "UploadToBufferObject"); |
Jason Sams | 93eacc7 | 2012-12-18 14:26:57 -0800 | [diff] [blame] | 250 | #endif |
Jason Sams | eb4fe18 | 2011-05-26 16:33:01 -0700 | [diff] [blame] | 251 | } |
| 252 | |
Tim Murray | 0b575de | 2013-03-15 15:56:43 -0700 | [diff] [blame] | 253 | |
Jason Sams | 9d8e5af | 2013-02-28 14:00:08 -0800 | [diff] [blame] | 254 | static size_t DeriveYUVLayout(int yuv, Allocation::Hal::DrvState *state) { |
Jason Sams | 733396b | 2013-02-22 12:46:18 -0800 | [diff] [blame] | 255 | // YUV only supports basic 2d |
| 256 | // so we can stash the plane pointers in the mipmap levels. |
Jason Sams | 9d8e5af | 2013-02-28 14:00:08 -0800 | [diff] [blame] | 257 | size_t uvSize = 0; |
Tim Murray | 0b575de | 2013-03-15 15:56:43 -0700 | [diff] [blame] | 258 | #ifndef RS_SERVER |
Jason Sams | 733396b | 2013-02-22 12:46:18 -0800 | [diff] [blame] | 259 | switch(yuv) { |
| 260 | case HAL_PIXEL_FORMAT_YV12: |
Jason Sams | 4961cce | 2013-04-11 16:11:46 -0700 | [diff] [blame] | 261 | state->lod[2].dimX = state->lod[0].dimX / 2; |
| 262 | state->lod[2].dimY = state->lod[0].dimY / 2; |
| 263 | state->lod[2].stride = rsRound(state->lod[0].stride >> 1, 16); |
| 264 | state->lod[2].mallocPtr = ((uint8_t *)state->lod[0].mallocPtr) + |
Jason Sams | 733396b | 2013-02-22 12:46:18 -0800 | [diff] [blame] | 265 | (state->lod[0].stride * state->lod[0].dimY); |
Jason Sams | 9d8e5af | 2013-02-28 14:00:08 -0800 | [diff] [blame] | 266 | uvSize += state->lod[2].stride * state->lod[2].dimY; |
Jason Sams | 733396b | 2013-02-22 12:46:18 -0800 | [diff] [blame] | 267 | |
Jason Sams | 4961cce | 2013-04-11 16:11:46 -0700 | [diff] [blame] | 268 | state->lod[1].dimX = state->lod[2].dimX; |
| 269 | state->lod[1].dimY = state->lod[2].dimY; |
| 270 | state->lod[1].stride = state->lod[2].stride; |
| 271 | state->lod[1].mallocPtr = ((uint8_t *)state->lod[2].mallocPtr) + |
| 272 | (state->lod[2].stride * state->lod[2].dimY); |
| 273 | uvSize += state->lod[1].stride * state->lod[2].dimY; |
| 274 | |
Jason Sams | 733396b | 2013-02-22 12:46:18 -0800 | [diff] [blame] | 275 | state->lodCount = 3; |
| 276 | break; |
| 277 | case HAL_PIXEL_FORMAT_YCrCb_420_SP: // NV21 |
| 278 | state->lod[1].dimX = state->lod[0].dimX; |
| 279 | state->lod[1].dimY = state->lod[0].dimY / 2; |
| 280 | state->lod[1].stride = state->lod[0].stride; |
| 281 | state->lod[1].mallocPtr = ((uint8_t *)state->lod[0].mallocPtr) + |
| 282 | (state->lod[0].stride * state->lod[0].dimY); |
Jason Sams | 9d8e5af | 2013-02-28 14:00:08 -0800 | [diff] [blame] | 283 | uvSize += state->lod[1].stride * state->lod[1].dimY; |
Jason Sams | 733396b | 2013-02-22 12:46:18 -0800 | [diff] [blame] | 284 | state->lodCount = 2; |
| 285 | break; |
| 286 | default: |
| 287 | rsAssert(0); |
| 288 | } |
Tim Murray | 0b575de | 2013-03-15 15:56:43 -0700 | [diff] [blame] | 289 | #endif |
Jason Sams | 9d8e5af | 2013-02-28 14:00:08 -0800 | [diff] [blame] | 290 | return uvSize; |
Jason Sams | 733396b | 2013-02-22 12:46:18 -0800 | [diff] [blame] | 291 | } |
| 292 | |
| 293 | |
Jason Sams | 463bfce | 2012-08-21 13:54:42 -0700 | [diff] [blame] | 294 | static size_t AllocationBuildPointerTable(const Context *rsc, const Allocation *alloc, |
| 295 | const Type *type, uint8_t *ptr) { |
Jason Sams | 709a097 | 2012-11-15 18:18:04 -0800 | [diff] [blame] | 296 | alloc->mHal.drvState.lod[0].dimX = type->getDimX(); |
| 297 | alloc->mHal.drvState.lod[0].dimY = type->getDimY(); |
Jason Sams | f2b611e | 2012-12-27 19:05:22 -0800 | [diff] [blame] | 298 | alloc->mHal.drvState.lod[0].dimZ = type->getDimZ(); |
Jason Sams | 709a097 | 2012-11-15 18:18:04 -0800 | [diff] [blame] | 299 | alloc->mHal.drvState.lod[0].mallocPtr = 0; |
Stephen Hines | 94999c3 | 2013-02-05 16:58:22 -0800 | [diff] [blame] | 300 | // Stride needs to be 16-byte aligned too! |
| 301 | size_t stride = alloc->mHal.drvState.lod[0].dimX * type->getElementSizeBytes(); |
| 302 | alloc->mHal.drvState.lod[0].stride = rsRound(stride, 16); |
Jason Sams | 709a097 | 2012-11-15 18:18:04 -0800 | [diff] [blame] | 303 | alloc->mHal.drvState.lodCount = type->getLODCount(); |
| 304 | alloc->mHal.drvState.faceCount = type->getDimFaces(); |
Jason Sams | 807fdc4 | 2012-07-25 17:55:39 -0700 | [diff] [blame] | 305 | |
| 306 | size_t offsets[Allocation::MAX_LOD]; |
| 307 | memset(offsets, 0, sizeof(offsets)); |
| 308 | |
Jason Sams | 709a097 | 2012-11-15 18:18:04 -0800 | [diff] [blame] | 309 | size_t o = alloc->mHal.drvState.lod[0].stride * rsMax(alloc->mHal.drvState.lod[0].dimY, 1u) * |
| 310 | rsMax(alloc->mHal.drvState.lod[0].dimZ, 1u); |
| 311 | if(alloc->mHal.drvState.lodCount > 1) { |
| 312 | uint32_t tx = alloc->mHal.drvState.lod[0].dimX; |
| 313 | uint32_t ty = alloc->mHal.drvState.lod[0].dimY; |
| 314 | uint32_t tz = alloc->mHal.drvState.lod[0].dimZ; |
| 315 | for (uint32_t lod=1; lod < alloc->mHal.drvState.lodCount; lod++) { |
| 316 | alloc->mHal.drvState.lod[lod].dimX = tx; |
| 317 | alloc->mHal.drvState.lod[lod].dimY = ty; |
| 318 | alloc->mHal.drvState.lod[lod].dimZ = tz; |
Stephen Hines | 94999c3 | 2013-02-05 16:58:22 -0800 | [diff] [blame] | 319 | alloc->mHal.drvState.lod[lod].stride = |
| 320 | rsRound(tx * type->getElementSizeBytes(), 16); |
Jason Sams | 807fdc4 | 2012-07-25 17:55:39 -0700 | [diff] [blame] | 321 | offsets[lod] = o; |
Jason Sams | 709a097 | 2012-11-15 18:18:04 -0800 | [diff] [blame] | 322 | 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] | 323 | if (tx > 1) tx >>= 1; |
| 324 | if (ty > 1) ty >>= 1; |
| 325 | if (tz > 1) tz >>= 1; |
| 326 | } |
Jason Sams | bc0ca6b | 2013-02-15 18:13:43 -0800 | [diff] [blame] | 327 | } else if (alloc->mHal.state.yuv) { |
Jason Sams | 9d8e5af | 2013-02-28 14:00:08 -0800 | [diff] [blame] | 328 | o += DeriveYUVLayout(alloc->mHal.state.yuv, &alloc->mHal.drvState); |
Jason Sams | 733396b | 2013-02-22 12:46:18 -0800 | [diff] [blame] | 329 | |
| 330 | for (uint32_t ct = 1; ct < alloc->mHal.drvState.lodCount; ct++) { |
| 331 | offsets[ct] = (size_t)alloc->mHal.drvState.lod[ct].mallocPtr; |
Jason Sams | bc0ca6b | 2013-02-15 18:13:43 -0800 | [diff] [blame] | 332 | } |
Jason Sams | 807fdc4 | 2012-07-25 17:55:39 -0700 | [diff] [blame] | 333 | } |
Jason Sams | bc0ca6b | 2013-02-15 18:13:43 -0800 | [diff] [blame] | 334 | |
Jason Sams | 709a097 | 2012-11-15 18:18:04 -0800 | [diff] [blame] | 335 | alloc->mHal.drvState.faceOffset = o; |
Jason Sams | 807fdc4 | 2012-07-25 17:55:39 -0700 | [diff] [blame] | 336 | |
Jason Sams | 709a097 | 2012-11-15 18:18:04 -0800 | [diff] [blame] | 337 | alloc->mHal.drvState.lod[0].mallocPtr = ptr; |
| 338 | for (uint32_t lod=1; lod < alloc->mHal.drvState.lodCount; lod++) { |
| 339 | alloc->mHal.drvState.lod[lod].mallocPtr = ptr + offsets[lod]; |
Jason Sams | 463bfce | 2012-08-21 13:54:42 -0700 | [diff] [blame] | 340 | } |
Jason Sams | 463bfce | 2012-08-21 13:54:42 -0700 | [diff] [blame] | 341 | |
Jason Sams | 709a097 | 2012-11-15 18:18:04 -0800 | [diff] [blame] | 342 | size_t allocSize = alloc->mHal.drvState.faceOffset; |
| 343 | if(alloc->mHal.drvState.faceCount) { |
Jason Sams | 463bfce | 2012-08-21 13:54:42 -0700 | [diff] [blame] | 344 | allocSize *= 6; |
| 345 | } |
| 346 | |
| 347 | return allocSize; |
| 348 | } |
| 349 | |
Tim Murray | c2cfe6a | 2013-02-14 16:21:33 -0800 | [diff] [blame] | 350 | static uint8_t* allocAlignedMemory(size_t allocSize, bool forceZero) { |
| 351 | // We align all allocations to a 16-byte boundary. |
| 352 | uint8_t* ptr = (uint8_t *)memalign(16, allocSize); |
| 353 | if (!ptr) { |
| 354 | return NULL; |
| 355 | } |
| 356 | if (forceZero) { |
| 357 | memset(ptr, 0, allocSize); |
| 358 | } |
| 359 | return ptr; |
| 360 | } |
| 361 | |
Jason Sams | 463bfce | 2012-08-21 13:54:42 -0700 | [diff] [blame] | 362 | bool rsdAllocationInit(const Context *rsc, Allocation *alloc, bool forceZero) { |
| 363 | DrvAllocation *drv = (DrvAllocation *)calloc(1, sizeof(DrvAllocation)); |
| 364 | if (!drv) { |
| 365 | return false; |
| 366 | } |
| 367 | alloc->mHal.drv = drv; |
| 368 | |
| 369 | // Calculate the object size. |
| 370 | size_t allocSize = AllocationBuildPointerTable(rsc, alloc, alloc->getType(), NULL); |
| 371 | |
Jason Sams | 807fdc4 | 2012-07-25 17:55:39 -0700 | [diff] [blame] | 372 | uint8_t * ptr = NULL; |
Jason Sams | 7ac2a4d | 2012-02-15 12:04:24 -0800 | [diff] [blame] | 373 | if (alloc->mHal.state.usageFlags & RS_ALLOCATION_USAGE_IO_OUTPUT) { |
Jason Sams | 733396b | 2013-02-22 12:46:18 -0800 | [diff] [blame] | 374 | |
| 375 | } else if (alloc->mHal.state.usageFlags & RS_ALLOCATION_USAGE_IO_INPUT) { |
| 376 | // Allocation is allocated when the surface is created |
| 377 | // in getSurface |
Tim Murray | 2e1a94d | 2012-11-29 13:12:25 -0800 | [diff] [blame] | 378 | } else if (alloc->mHal.state.userProvidedPtr != NULL) { |
| 379 | // user-provided allocation |
Tim Murray | ba24d08 | 2013-04-09 17:31:11 -0700 | [diff] [blame] | 380 | // limitations: no faces, no LOD, USAGE_SCRIPT or SCRIPT+TEXTURE only |
| 381 | if (!(alloc->mHal.state.usageFlags == (RS_ALLOCATION_USAGE_SCRIPT | RS_ALLOCATION_USAGE_SHARED) || |
| 382 | alloc->mHal.state.usageFlags == (RS_ALLOCATION_USAGE_SCRIPT | RS_ALLOCATION_USAGE_SHARED | RS_ALLOCATION_USAGE_GRAPHICS_TEXTURE))) { |
| 383 | ALOGE("Can't use user-allocated buffers if usage is not USAGE_SCRIPT | USAGE_SHARED or USAGE_SCRIPT | USAGE_SHARED | USAGE_GRAPHICS_TEXTURE"); |
Tim Murray | 2e1a94d | 2012-11-29 13:12:25 -0800 | [diff] [blame] | 384 | return false; |
| 385 | } |
| 386 | if (alloc->getType()->getDimLOD() || alloc->getType()->getDimFaces()) { |
| 387 | ALOGE("User-allocated buffers must not have multiple faces or LODs"); |
| 388 | return false; |
| 389 | } |
Tim Murray | c2cfe6a | 2013-02-14 16:21:33 -0800 | [diff] [blame] | 390 | |
| 391 | // rows must be 16-byte aligned |
| 392 | // validate that here, otherwise fall back to not use the user-backed allocation |
| 393 | if (((alloc->getType()->getDimX() * alloc->getType()->getElement()->getSizeBytes()) % 16) != 0) { |
| 394 | ALOGV("User-backed allocation failed stride requirement, falling back to separate allocation"); |
| 395 | drv->useUserProvidedPtr = false; |
| 396 | |
| 397 | ptr = allocAlignedMemory(allocSize, forceZero); |
| 398 | if (!ptr) { |
| 399 | alloc->mHal.drv = NULL; |
| 400 | free(drv); |
| 401 | return false; |
| 402 | } |
| 403 | |
| 404 | } else { |
| 405 | drv->useUserProvidedPtr = true; |
| 406 | ptr = (uint8_t*)alloc->mHal.state.userProvidedPtr; |
| 407 | } |
Jason Sams | 7ac2a4d | 2012-02-15 12:04:24 -0800 | [diff] [blame] | 408 | } else { |
Tim Murray | c2cfe6a | 2013-02-14 16:21:33 -0800 | [diff] [blame] | 409 | ptr = allocAlignedMemory(allocSize, forceZero); |
Jason Sams | 179e9a4 | 2011-11-23 15:02:15 -0800 | [diff] [blame] | 410 | if (!ptr) { |
You Kim | ed419f3 | 2012-12-17 03:42:57 +0900 | [diff] [blame] | 411 | alloc->mHal.drv = NULL; |
Jason Sams | 179e9a4 | 2011-11-23 15:02:15 -0800 | [diff] [blame] | 412 | free(drv); |
| 413 | return false; |
| 414 | } |
Jason Sams | eb4fe18 | 2011-05-26 16:33:01 -0700 | [diff] [blame] | 415 | } |
Jason Sams | 463bfce | 2012-08-21 13:54:42 -0700 | [diff] [blame] | 416 | // Build the pointer tables |
| 417 | size_t verifySize = AllocationBuildPointerTable(rsc, alloc, alloc->getType(), ptr); |
| 418 | if(allocSize != verifySize) { |
| 419 | rsAssert(!"Size mismatch"); |
Jason Sams | 807fdc4 | 2012-07-25 17:55:39 -0700 | [diff] [blame] | 420 | } |
Jason Sams | eb4fe18 | 2011-05-26 16:33:01 -0700 | [diff] [blame] | 421 | |
Tim Murray | 0b575de | 2013-03-15 15:56:43 -0700 | [diff] [blame] | 422 | #ifndef RS_SERVER |
Jason Sams | eb4fe18 | 2011-05-26 16:33:01 -0700 | [diff] [blame] | 423 | drv->glTarget = GL_NONE; |
| 424 | if (alloc->mHal.state.usageFlags & RS_ALLOCATION_USAGE_GRAPHICS_TEXTURE) { |
| 425 | if (alloc->mHal.state.hasFaces) { |
| 426 | drv->glTarget = GL_TEXTURE_CUBE_MAP; |
| 427 | } else { |
| 428 | drv->glTarget = GL_TEXTURE_2D; |
| 429 | } |
| 430 | } else { |
| 431 | if (alloc->mHal.state.usageFlags & RS_ALLOCATION_USAGE_GRAPHICS_VERTEX) { |
| 432 | drv->glTarget = GL_ARRAY_BUFFER; |
| 433 | } |
| 434 | } |
Tim Murray | 0b575de | 2013-03-15 15:56:43 -0700 | [diff] [blame] | 435 | #endif |
Jason Sams | eb4fe18 | 2011-05-26 16:33:01 -0700 | [diff] [blame] | 436 | |
Jason Sams | 93eacc7 | 2012-12-18 14:26:57 -0800 | [diff] [blame] | 437 | #ifndef RS_COMPATIBILITY_LIB |
Jason Sams | a614ae1 | 2011-05-26 17:05:51 -0700 | [diff] [blame] | 438 | drv->glType = rsdTypeToGLType(alloc->mHal.state.type->getElement()->getComponent().getType()); |
| 439 | drv->glFormat = rsdKindToGLFormat(alloc->mHal.state.type->getElement()->getComponent().getKind()); |
Jason Sams | 93eacc7 | 2012-12-18 14:26:57 -0800 | [diff] [blame] | 440 | #else |
| 441 | drv->glType = 0; |
| 442 | drv->glFormat = 0; |
| 443 | #endif |
Jason Sams | a614ae1 | 2011-05-26 17:05:51 -0700 | [diff] [blame] | 444 | |
Jason Sams | eb4fe18 | 2011-05-26 16:33:01 -0700 | [diff] [blame] | 445 | if (alloc->mHal.state.usageFlags & ~RS_ALLOCATION_USAGE_SCRIPT) { |
| 446 | drv->uploadDeferred = true; |
| 447 | } |
Alex Sakhartchouk | a949524 | 2011-06-16 11:05:13 -0700 | [diff] [blame] | 448 | |
Jason Sams | b322033 | 2012-04-02 14:41:54 -0700 | [diff] [blame] | 449 | |
Alex Sakhartchouk | a949524 | 2011-06-16 11:05:13 -0700 | [diff] [blame] | 450 | drv->readBackFBO = NULL; |
| 451 | |
Tim Murray | c2cfe6a | 2013-02-14 16:21:33 -0800 | [diff] [blame] | 452 | // fill out the initial state of the buffer if we couldn't use the user-provided ptr and USAGE_SHARED was accepted |
| 453 | if ((alloc->mHal.state.userProvidedPtr != 0) && (drv->useUserProvidedPtr == false)) { |
| 454 | rsdAllocationData2D(rsc, alloc, 0, 0, 0, RS_ALLOCATION_CUBEMAP_FACE_POSITIVE_X, alloc->getType()->getDimX(), alloc->getType()->getDimY(), alloc->mHal.state.userProvidedPtr, allocSize, 0); |
| 455 | } |
| 456 | |
Jason Sams | eb4fe18 | 2011-05-26 16:33:01 -0700 | [diff] [blame] | 457 | return true; |
| 458 | } |
| 459 | |
| 460 | void rsdAllocationDestroy(const Context *rsc, Allocation *alloc) { |
| 461 | DrvAllocation *drv = (DrvAllocation *)alloc->mHal.drv; |
| 462 | |
Jason Sams | 93eacc7 | 2012-12-18 14:26:57 -0800 | [diff] [blame] | 463 | #ifndef RS_COMPATIBILITY_LIB |
Jason Sams | eb4fe18 | 2011-05-26 16:33:01 -0700 | [diff] [blame] | 464 | if (drv->bufferID) { |
| 465 | // Causes a SW crash.... |
Steve Block | 6598201 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 466 | //ALOGV(" mBufferID %i", mBufferID); |
Jason Sams | eb4fe18 | 2011-05-26 16:33:01 -0700 | [diff] [blame] | 467 | //glDeleteBuffers(1, &mBufferID); |
| 468 | //mBufferID = 0; |
| 469 | } |
| 470 | if (drv->textureID) { |
Jason Sams | 2382aba | 2011-09-13 15:41:01 -0700 | [diff] [blame] | 471 | RSD_CALL_GL(glDeleteTextures, 1, &drv->textureID); |
Jason Sams | eb4fe18 | 2011-05-26 16:33:01 -0700 | [diff] [blame] | 472 | drv->textureID = 0; |
| 473 | } |
| 474 | if (drv->renderTargetID) { |
Jason Sams | 2382aba | 2011-09-13 15:41:01 -0700 | [diff] [blame] | 475 | RSD_CALL_GL(glDeleteRenderbuffers, 1, &drv->renderTargetID); |
Jason Sams | eb4fe18 | 2011-05-26 16:33:01 -0700 | [diff] [blame] | 476 | drv->renderTargetID = 0; |
| 477 | } |
Jason Sams | 93eacc7 | 2012-12-18 14:26:57 -0800 | [diff] [blame] | 478 | #endif |
Jason Sams | eb4fe18 | 2011-05-26 16:33:01 -0700 | [diff] [blame] | 479 | |
Jason Sams | 709a097 | 2012-11-15 18:18:04 -0800 | [diff] [blame] | 480 | if (alloc->mHal.drvState.lod[0].mallocPtr) { |
Tim Murray | aafa7a8 | 2013-05-09 13:22:53 -0700 | [diff] [blame] | 481 | // don't free user-allocated ptrs or IO_OUTPUT buffers |
| 482 | if (!(drv->useUserProvidedPtr) && |
Sakshi Agrawal | c88bcef | 2013-05-13 16:46:09 -0700 | [diff] [blame] | 483 | !(alloc->mHal.state.usageFlags & RS_ALLOCATION_USAGE_IO_INPUT) && |
Tim Murray | aafa7a8 | 2013-05-09 13:22:53 -0700 | [diff] [blame] | 484 | !(alloc->mHal.state.usageFlags & RS_ALLOCATION_USAGE_IO_OUTPUT)) { |
Sakshi Agrawal | c88bcef | 2013-05-13 16:46:09 -0700 | [diff] [blame] | 485 | free(alloc->mHal.drvState.lod[0].mallocPtr); |
Tim Murray | 2e1a94d | 2012-11-29 13:12:25 -0800 | [diff] [blame] | 486 | } |
Jason Sams | 709a097 | 2012-11-15 18:18:04 -0800 | [diff] [blame] | 487 | alloc->mHal.drvState.lod[0].mallocPtr = NULL; |
Jason Sams | eb4fe18 | 2011-05-26 16:33:01 -0700 | [diff] [blame] | 488 | } |
Jason Sams | 93eacc7 | 2012-12-18 14:26:57 -0800 | [diff] [blame] | 489 | |
| 490 | #ifndef RS_COMPATIBILITY_LIB |
Alex Sakhartchouk | a949524 | 2011-06-16 11:05:13 -0700 | [diff] [blame] | 491 | if (drv->readBackFBO != NULL) { |
| 492 | delete drv->readBackFBO; |
| 493 | drv->readBackFBO = NULL; |
| 494 | } |
Jason Sams | 10c9dd7 | 2013-02-01 10:53:42 -0800 | [diff] [blame] | 495 | |
| 496 | if ((alloc->mHal.state.usageFlags & RS_ALLOCATION_USAGE_IO_OUTPUT) && |
| 497 | (alloc->mHal.state.usageFlags & RS_ALLOCATION_USAGE_SCRIPT)) { |
| 498 | |
| 499 | DrvAllocation *drv = (DrvAllocation *)alloc->mHal.drv; |
Tim Murray | 3a25fdd | 2013-02-22 17:56:56 -0800 | [diff] [blame] | 500 | ANativeWindow *nw = drv->wndSurface; |
| 501 | if (nw) { |
| 502 | GraphicBufferMapper &mapper = GraphicBufferMapper::get(); |
| 503 | mapper.unlock(drv->wndBuffer->handle); |
| 504 | int32_t r = nw->queueBuffer(nw, drv->wndBuffer, -1); |
| 505 | } |
Jason Sams | 10c9dd7 | 2013-02-01 10:53:42 -0800 | [diff] [blame] | 506 | } |
Jason Sams | 93eacc7 | 2012-12-18 14:26:57 -0800 | [diff] [blame] | 507 | #endif |
| 508 | |
Jason Sams | eb4fe18 | 2011-05-26 16:33:01 -0700 | [diff] [blame] | 509 | free(drv); |
| 510 | alloc->mHal.drv = NULL; |
| 511 | } |
| 512 | |
| 513 | void rsdAllocationResize(const Context *rsc, const Allocation *alloc, |
| 514 | const Type *newType, bool zeroNew) { |
Jason Sams | a572aca | 2013-01-09 11:52:26 -0800 | [diff] [blame] | 515 | const uint32_t oldDimX = alloc->mHal.drvState.lod[0].dimX; |
| 516 | const uint32_t dimX = newType->getDimX(); |
| 517 | |
Tim Murray | 9e2bda5 | 2012-12-18 12:05:46 -0800 | [diff] [blame] | 518 | // can't resize Allocations with user-allocated buffers |
| 519 | if (alloc->mHal.state.usageFlags & RS_ALLOCATION_USAGE_SHARED) { |
| 520 | ALOGE("Resize cannot be called on a USAGE_SHARED allocation"); |
| 521 | return; |
| 522 | } |
Jason Sams | 709a097 | 2012-11-15 18:18:04 -0800 | [diff] [blame] | 523 | void * oldPtr = alloc->mHal.drvState.lod[0].mallocPtr; |
Jason Sams | 463bfce | 2012-08-21 13:54:42 -0700 | [diff] [blame] | 524 | // Calculate the object size |
| 525 | size_t s = AllocationBuildPointerTable(rsc, alloc, newType, NULL); |
| 526 | uint8_t *ptr = (uint8_t *)realloc(oldPtr, s); |
| 527 | // Build the relative pointer tables. |
| 528 | size_t verifySize = AllocationBuildPointerTable(rsc, alloc, newType, ptr); |
| 529 | if(s != verifySize) { |
| 530 | rsAssert(!"Size mismatch"); |
| 531 | } |
Jason Sams | eb4fe18 | 2011-05-26 16:33:01 -0700 | [diff] [blame] | 532 | |
Jason Sams | eb4fe18 | 2011-05-26 16:33:01 -0700 | [diff] [blame] | 533 | |
| 534 | if (dimX > oldDimX) { |
Tim Murray | 099bc26 | 2013-03-20 16:54:03 -0700 | [diff] [blame] | 535 | size_t stride = alloc->mHal.state.elementSizeBytes; |
Jason Sams | 709a097 | 2012-11-15 18:18:04 -0800 | [diff] [blame] | 536 | memset(((uint8_t *)alloc->mHal.drvState.lod[0].mallocPtr) + stride * oldDimX, |
Jason Sams | 2275e9c | 2012-04-16 17:01:26 -0700 | [diff] [blame] | 537 | 0, stride * (dimX - oldDimX)); |
Jason Sams | eb4fe18 | 2011-05-26 16:33:01 -0700 | [diff] [blame] | 538 | } |
| 539 | } |
| 540 | |
Alex Sakhartchouk | a949524 | 2011-06-16 11:05:13 -0700 | [diff] [blame] | 541 | static void rsdAllocationSyncFromFBO(const Context *rsc, const Allocation *alloc) { |
Jason Sams | 93eacc7 | 2012-12-18 14:26:57 -0800 | [diff] [blame] | 542 | #ifndef RS_COMPATIBILITY_LIB |
Alex Sakhartchouk | a949524 | 2011-06-16 11:05:13 -0700 | [diff] [blame] | 543 | if (!alloc->getIsScript()) { |
| 544 | return; // nothing to sync |
| 545 | } |
| 546 | |
| 547 | RsdHal *dc = (RsdHal *)rsc->mHal.drv; |
| 548 | RsdFrameBufferObj *lastFbo = dc->gl.currentFrameBuffer; |
| 549 | |
| 550 | DrvAllocation *drv = (DrvAllocation *)alloc->mHal.drv; |
| 551 | if (!drv->textureID && !drv->renderTargetID) { |
| 552 | return; // nothing was rendered here yet, so nothing to sync |
| 553 | } |
| 554 | if (drv->readBackFBO == NULL) { |
| 555 | drv->readBackFBO = new RsdFrameBufferObj(); |
| 556 | drv->readBackFBO->setColorTarget(drv, 0); |
| 557 | drv->readBackFBO->setDimensions(alloc->getType()->getDimX(), |
| 558 | alloc->getType()->getDimY()); |
| 559 | } |
| 560 | |
| 561 | // Bind the framebuffer object so we can read back from it |
| 562 | drv->readBackFBO->setActive(rsc); |
| 563 | |
| 564 | // Do the readback |
Jason Sams | 709a097 | 2012-11-15 18:18:04 -0800 | [diff] [blame] | 565 | RSD_CALL_GL(glReadPixels, 0, 0, alloc->mHal.drvState.lod[0].dimX, |
| 566 | alloc->mHal.drvState.lod[0].dimY, |
| 567 | drv->glFormat, drv->glType, alloc->mHal.drvState.lod[0].mallocPtr); |
Alex Sakhartchouk | a949524 | 2011-06-16 11:05:13 -0700 | [diff] [blame] | 568 | |
| 569 | // Revert framebuffer to its original |
| 570 | lastFbo->setActive(rsc); |
Jason Sams | 93eacc7 | 2012-12-18 14:26:57 -0800 | [diff] [blame] | 571 | #endif |
Alex Sakhartchouk | a949524 | 2011-06-16 11:05:13 -0700 | [diff] [blame] | 572 | } |
Jason Sams | eb4fe18 | 2011-05-26 16:33:01 -0700 | [diff] [blame] | 573 | |
| 574 | |
| 575 | void rsdAllocationSyncAll(const Context *rsc, const Allocation *alloc, |
| 576 | RsAllocationUsageType src) { |
| 577 | DrvAllocation *drv = (DrvAllocation *)alloc->mHal.drv; |
| 578 | |
Alex Sakhartchouk | a949524 | 2011-06-16 11:05:13 -0700 | [diff] [blame] | 579 | if (src == RS_ALLOCATION_USAGE_GRAPHICS_RENDER_TARGET) { |
| 580 | if(!alloc->getIsRenderTarget()) { |
| 581 | rsc->setError(RS_ERROR_FATAL_DRIVER, |
| 582 | "Attempting to sync allocation from render target, " |
| 583 | "for non-render target allocation"); |
| 584 | } else if (alloc->getType()->getElement()->getKind() != RS_KIND_PIXEL_RGBA) { |
| 585 | rsc->setError(RS_ERROR_FATAL_DRIVER, "Cannot only sync from RGBA" |
| 586 | "render target"); |
| 587 | } else { |
| 588 | rsdAllocationSyncFromFBO(rsc, alloc); |
| 589 | } |
Jason Sams | eb4fe18 | 2011-05-26 16:33:01 -0700 | [diff] [blame] | 590 | return; |
| 591 | } |
| 592 | |
| 593 | rsAssert(src == RS_ALLOCATION_USAGE_SCRIPT); |
| 594 | |
| 595 | if (alloc->mHal.state.usageFlags & RS_ALLOCATION_USAGE_GRAPHICS_TEXTURE) { |
| 596 | UploadToTexture(rsc, alloc); |
| 597 | } else { |
Jason Sams | b322033 | 2012-04-02 14:41:54 -0700 | [diff] [blame] | 598 | if ((alloc->mHal.state.usageFlags & RS_ALLOCATION_USAGE_GRAPHICS_RENDER_TARGET) && |
Jason Sams | 0dc6693 | 2012-04-10 17:05:49 -0700 | [diff] [blame] | 599 | !(alloc->mHal.state.usageFlags & RS_ALLOCATION_USAGE_IO_OUTPUT)) { |
Jason Sams | eb4fe18 | 2011-05-26 16:33:01 -0700 | [diff] [blame] | 600 | AllocateRenderTarget(rsc, alloc); |
| 601 | } |
| 602 | } |
| 603 | if (alloc->mHal.state.usageFlags & RS_ALLOCATION_USAGE_GRAPHICS_VERTEX) { |
| 604 | UploadToBufferObject(rsc, alloc); |
| 605 | } |
| 606 | |
Tim Murray | ba24d08 | 2013-04-09 17:31:11 -0700 | [diff] [blame] | 607 | if (alloc->mHal.state.usageFlags & RS_ALLOCATION_USAGE_SHARED) { |
| 608 | // NOP in CPU driver for now |
| 609 | } |
| 610 | |
Jason Sams | eb4fe18 | 2011-05-26 16:33:01 -0700 | [diff] [blame] | 611 | drv->uploadDeferred = false; |
| 612 | } |
| 613 | |
| 614 | void rsdAllocationMarkDirty(const Context *rsc, const Allocation *alloc) { |
| 615 | DrvAllocation *drv = (DrvAllocation *)alloc->mHal.drv; |
| 616 | drv->uploadDeferred = true; |
| 617 | } |
| 618 | |
Jason Sams | 4961cce | 2013-04-11 16:11:46 -0700 | [diff] [blame] | 619 | #ifndef RS_COMPATIBILITY_LIB |
| 620 | void DrvAllocation::NewBufferListener::onFrameAvailable() { |
| 621 | intptr_t ip = (intptr_t)alloc; |
| 622 | rsc->sendMessageToClient(NULL, RS_MESSAGE_TO_CLIENT_NEW_BUFFER, ip, 0, true); |
| 623 | } |
| 624 | #endif |
| 625 | |
Jason Sams | 733396b | 2013-02-22 12:46:18 -0800 | [diff] [blame] | 626 | void* rsdAllocationGetSurface(const Context *rsc, const Allocation *alloc) { |
Jason Sams | 93eacc7 | 2012-12-18 14:26:57 -0800 | [diff] [blame] | 627 | #ifndef RS_COMPATIBILITY_LIB |
Jason Sams | 41e373d | 2012-01-13 14:01:20 -0800 | [diff] [blame] | 628 | DrvAllocation *drv = (DrvAllocation *)alloc->mHal.drv; |
Jason Sams | 733396b | 2013-02-22 12:46:18 -0800 | [diff] [blame] | 629 | |
Eino-Ville Talvala | 0d44f12 | 2013-04-03 16:42:35 -0700 | [diff] [blame] | 630 | // Configure CpuConsumer to be in asynchronous mode |
Mathias Agopian | d232558 | 2013-07-12 22:06:38 -0700 | [diff] [blame] | 631 | sp<BufferQueue> bq = new BufferQueue(); |
| 632 | drv->cpuConsumer = new CpuConsumer(bq, 2, false); |
Mathias Agopian | 126adc5 | 2013-08-02 02:15:17 -0700 | [diff] [blame^] | 633 | sp<IGraphicBufferProducer> bp = bq; |
Jason Sams | 733396b | 2013-02-22 12:46:18 -0800 | [diff] [blame] | 634 | bp->incStrong(NULL); |
Jason Sams | 4961cce | 2013-04-11 16:11:46 -0700 | [diff] [blame] | 635 | |
| 636 | drv->mBufferListener = new DrvAllocation::NewBufferListener(); |
| 637 | drv->mBufferListener->rsc = rsc; |
| 638 | drv->mBufferListener->alloc = alloc; |
| 639 | |
| 640 | drv->cpuConsumer->setFrameAvailableListener(drv->mBufferListener); |
| 641 | |
Jason Sams | 733396b | 2013-02-22 12:46:18 -0800 | [diff] [blame] | 642 | return bp.get(); |
Jason Sams | 93eacc7 | 2012-12-18 14:26:57 -0800 | [diff] [blame] | 643 | #else |
Jason Sams | 733396b | 2013-02-22 12:46:18 -0800 | [diff] [blame] | 644 | return NULL; |
Jason Sams | 93eacc7 | 2012-12-18 14:26:57 -0800 | [diff] [blame] | 645 | #endif |
Jason Sams | 41e373d | 2012-01-13 14:01:20 -0800 | [diff] [blame] | 646 | } |
| 647 | |
Jason Sams | 93eacc7 | 2012-12-18 14:26:57 -0800 | [diff] [blame] | 648 | #ifndef RS_COMPATIBILITY_LIB |
Jason Sams | 7ac2a4d | 2012-02-15 12:04:24 -0800 | [diff] [blame] | 649 | static bool IoGetBuffer(const Context *rsc, Allocation *alloc, ANativeWindow *nw) { |
| 650 | DrvAllocation *drv = (DrvAllocation *)alloc->mHal.drv; |
| 651 | |
Jamie Gennis | cd919a1 | 2012-06-13 16:34:11 -0700 | [diff] [blame] | 652 | int32_t r = native_window_dequeue_buffer_and_wait(nw, &drv->wndBuffer); |
Jason Sams | 7ac2a4d | 2012-02-15 12:04:24 -0800 | [diff] [blame] | 653 | if (r) { |
| 654 | rsc->setError(RS_ERROR_DRIVER, "Error getting next IO output buffer."); |
| 655 | return false; |
| 656 | } |
| 657 | |
Jason Sams | 7ac2a4d | 2012-02-15 12:04:24 -0800 | [diff] [blame] | 658 | // Must lock the whole surface |
| 659 | GraphicBufferMapper &mapper = GraphicBufferMapper::get(); |
| 660 | Rect bounds(drv->wndBuffer->width, drv->wndBuffer->height); |
| 661 | |
| 662 | void *dst = NULL; |
| 663 | mapper.lock(drv->wndBuffer->handle, |
| 664 | GRALLOC_USAGE_SW_READ_NEVER | GRALLOC_USAGE_SW_WRITE_OFTEN, |
| 665 | bounds, &dst); |
Jason Sams | 709a097 | 2012-11-15 18:18:04 -0800 | [diff] [blame] | 666 | alloc->mHal.drvState.lod[0].mallocPtr = dst; |
| 667 | alloc->mHal.drvState.lod[0].stride = drv->wndBuffer->stride * alloc->mHal.state.elementSizeBytes; |
Stephen Hines | 94999c3 | 2013-02-05 16:58:22 -0800 | [diff] [blame] | 668 | rsAssert((alloc->mHal.drvState.lod[0].stride & 0xf) == 0); |
Jason Sams | b322033 | 2012-04-02 14:41:54 -0700 | [diff] [blame] | 669 | |
Jason Sams | 7ac2a4d | 2012-02-15 12:04:24 -0800 | [diff] [blame] | 670 | return true; |
| 671 | } |
Jason Sams | 93eacc7 | 2012-12-18 14:26:57 -0800 | [diff] [blame] | 672 | #endif |
Jason Sams | 7ac2a4d | 2012-02-15 12:04:24 -0800 | [diff] [blame] | 673 | |
Jason Sams | 733396b | 2013-02-22 12:46:18 -0800 | [diff] [blame] | 674 | void rsdAllocationSetSurface(const Context *rsc, Allocation *alloc, ANativeWindow *nw) { |
Jason Sams | 93eacc7 | 2012-12-18 14:26:57 -0800 | [diff] [blame] | 675 | #ifndef RS_COMPATIBILITY_LIB |
Jason Sams | 7ac2a4d | 2012-02-15 12:04:24 -0800 | [diff] [blame] | 676 | DrvAllocation *drv = (DrvAllocation *)alloc->mHal.drv; |
Tim Murray | 3a25fdd | 2013-02-22 17:56:56 -0800 | [diff] [blame] | 677 | ANativeWindow *old = drv->wndSurface; |
| 678 | |
| 679 | if (nw) { |
| 680 | nw->incStrong(NULL); |
| 681 | } |
Jason Sams | 7ac2a4d | 2012-02-15 12:04:24 -0800 | [diff] [blame] | 682 | |
Jason Sams | b322033 | 2012-04-02 14:41:54 -0700 | [diff] [blame] | 683 | if (alloc->mHal.state.usageFlags & RS_ALLOCATION_USAGE_GRAPHICS_RENDER_TARGET) { |
| 684 | //TODO finish support for render target + script |
| 685 | drv->wnd = nw; |
| 686 | return; |
| 687 | } |
| 688 | |
Jason Sams | 7ac2a4d | 2012-02-15 12:04:24 -0800 | [diff] [blame] | 689 | // Cleanup old surface if there is one. |
Tim Murray | 3a25fdd | 2013-02-22 17:56:56 -0800 | [diff] [blame] | 690 | if (drv->wndSurface) { |
| 691 | ANativeWindow *old = drv->wndSurface; |
Jason Sams | 7ac2a4d | 2012-02-15 12:04:24 -0800 | [diff] [blame] | 692 | GraphicBufferMapper &mapper = GraphicBufferMapper::get(); |
| 693 | mapper.unlock(drv->wndBuffer->handle); |
Tim Murray | 3a25fdd | 2013-02-22 17:56:56 -0800 | [diff] [blame] | 694 | old->cancelBuffer(old, drv->wndBuffer, -1); |
| 695 | drv->wndSurface = NULL; |
| 696 | old->decStrong(NULL); |
Jason Sams | 7ac2a4d | 2012-02-15 12:04:24 -0800 | [diff] [blame] | 697 | } |
| 698 | |
| 699 | if (nw != NULL) { |
| 700 | int32_t r; |
Jason Sams | b322033 | 2012-04-02 14:41:54 -0700 | [diff] [blame] | 701 | uint32_t flags = 0; |
Tim Murray | 3a25fdd | 2013-02-22 17:56:56 -0800 | [diff] [blame] | 702 | |
Jason Sams | b322033 | 2012-04-02 14:41:54 -0700 | [diff] [blame] | 703 | if (alloc->mHal.state.usageFlags & RS_ALLOCATION_USAGE_SCRIPT) { |
| 704 | flags |= GRALLOC_USAGE_SW_READ_RARELY | GRALLOC_USAGE_SW_WRITE_OFTEN; |
| 705 | } |
| 706 | if (alloc->mHal.state.usageFlags & RS_ALLOCATION_USAGE_GRAPHICS_RENDER_TARGET) { |
| 707 | flags |= GRALLOC_USAGE_HW_RENDER; |
| 708 | } |
| 709 | |
| 710 | r = native_window_set_usage(nw, flags); |
Jason Sams | 7ac2a4d | 2012-02-15 12:04:24 -0800 | [diff] [blame] | 711 | if (r) { |
| 712 | rsc->setError(RS_ERROR_DRIVER, "Error setting IO output buffer usage."); |
Tim Murray | 3a25fdd | 2013-02-22 17:56:56 -0800 | [diff] [blame] | 713 | goto error; |
Jason Sams | 7ac2a4d | 2012-02-15 12:04:24 -0800 | [diff] [blame] | 714 | } |
| 715 | |
Jason Sams | a572aca | 2013-01-09 11:52:26 -0800 | [diff] [blame] | 716 | r = native_window_set_buffers_dimensions(nw, alloc->mHal.drvState.lod[0].dimX, |
| 717 | alloc->mHal.drvState.lod[0].dimY); |
Jason Sams | 7ac2a4d | 2012-02-15 12:04:24 -0800 | [diff] [blame] | 718 | if (r) { |
| 719 | rsc->setError(RS_ERROR_DRIVER, "Error setting IO output buffer dimensions."); |
Tim Murray | 3a25fdd | 2013-02-22 17:56:56 -0800 | [diff] [blame] | 720 | goto error; |
Jason Sams | 7ac2a4d | 2012-02-15 12:04:24 -0800 | [diff] [blame] | 721 | } |
| 722 | |
Jason Sams | 733396b | 2013-02-22 12:46:18 -0800 | [diff] [blame] | 723 | int format = 0; |
| 724 | const Element *e = alloc->mHal.state.type->getElement(); |
Stephen Hines | 8b9b3aa | 2013-08-02 15:51:36 -0700 | [diff] [blame] | 725 | rsAssert(e->getType() == RS_TYPE_UNSIGNED_8); |
| 726 | rsAssert(e->getVectorSize() == 4); |
| 727 | rsAssert(e->getKind() == RS_KIND_PIXEL_RGBA); |
| 728 | format = PIXEL_FORMAT_RGBA_8888; |
Jason Sams | 733396b | 2013-02-22 12:46:18 -0800 | [diff] [blame] | 729 | |
| 730 | r = native_window_set_buffers_format(nw, format); |
| 731 | if (r) { |
| 732 | rsc->setError(RS_ERROR_DRIVER, "Error setting IO output buffer format."); |
Tim Murray | 3a25fdd | 2013-02-22 17:56:56 -0800 | [diff] [blame] | 733 | goto error; |
Jason Sams | 7ac2a4d | 2012-02-15 12:04:24 -0800 | [diff] [blame] | 734 | } |
| 735 | |
| 736 | IoGetBuffer(rsc, alloc, nw); |
Tim Murray | 3a25fdd | 2013-02-22 17:56:56 -0800 | [diff] [blame] | 737 | drv->wndSurface = nw; |
Jason Sams | 7ac2a4d | 2012-02-15 12:04:24 -0800 | [diff] [blame] | 738 | } |
Tim Murray | 3a25fdd | 2013-02-22 17:56:56 -0800 | [diff] [blame] | 739 | |
| 740 | return; |
| 741 | |
| 742 | error: |
| 743 | |
| 744 | if (nw) { |
| 745 | nw->decStrong(NULL); |
| 746 | } |
| 747 | |
| 748 | |
Jason Sams | 93eacc7 | 2012-12-18 14:26:57 -0800 | [diff] [blame] | 749 | #endif |
Jason Sams | 7ac2a4d | 2012-02-15 12:04:24 -0800 | [diff] [blame] | 750 | } |
| 751 | |
| 752 | void rsdAllocationIoSend(const Context *rsc, Allocation *alloc) { |
Jason Sams | 93eacc7 | 2012-12-18 14:26:57 -0800 | [diff] [blame] | 753 | #ifndef RS_COMPATIBILITY_LIB |
Jason Sams | 7ac2a4d | 2012-02-15 12:04:24 -0800 | [diff] [blame] | 754 | DrvAllocation *drv = (DrvAllocation *)alloc->mHal.drv; |
Tim Murray | 3a25fdd | 2013-02-22 17:56:56 -0800 | [diff] [blame] | 755 | ANativeWindow *nw = drv->wndSurface; |
Jason Sams | b322033 | 2012-04-02 14:41:54 -0700 | [diff] [blame] | 756 | if (alloc->mHal.state.usageFlags & RS_ALLOCATION_USAGE_GRAPHICS_RENDER_TARGET) { |
| 757 | RsdHal *dc = (RsdHal *)rsc->mHal.drv; |
| 758 | RSD_CALL_GL(eglSwapBuffers, dc->gl.egl.display, dc->gl.egl.surface); |
Jason Sams | 7ac2a4d | 2012-02-15 12:04:24 -0800 | [diff] [blame] | 759 | return; |
| 760 | } |
Tim Murray | 3a25fdd | 2013-02-22 17:56:56 -0800 | [diff] [blame] | 761 | if (nw) { |
| 762 | if (alloc->mHal.state.usageFlags & RS_ALLOCATION_USAGE_SCRIPT) { |
| 763 | GraphicBufferMapper &mapper = GraphicBufferMapper::get(); |
| 764 | mapper.unlock(drv->wndBuffer->handle); |
| 765 | int32_t r = nw->queueBuffer(nw, drv->wndBuffer, -1); |
| 766 | if (r) { |
| 767 | rsc->setError(RS_ERROR_DRIVER, "Error sending IO output buffer."); |
| 768 | return; |
| 769 | } |
Jason Sams | 7ac2a4d | 2012-02-15 12:04:24 -0800 | [diff] [blame] | 770 | |
Tim Murray | 3a25fdd | 2013-02-22 17:56:56 -0800 | [diff] [blame] | 771 | IoGetBuffer(rsc, alloc, nw); |
Jason Sams | b322033 | 2012-04-02 14:41:54 -0700 | [diff] [blame] | 772 | } |
Tim Murray | 3a25fdd | 2013-02-22 17:56:56 -0800 | [diff] [blame] | 773 | } else { |
| 774 | rsc->setError(RS_ERROR_DRIVER, "Sent IO buffer with no attached surface."); |
| 775 | return; |
Jason Sams | b322033 | 2012-04-02 14:41:54 -0700 | [diff] [blame] | 776 | } |
Jason Sams | 93eacc7 | 2012-12-18 14:26:57 -0800 | [diff] [blame] | 777 | #endif |
Jason Sams | 7ac2a4d | 2012-02-15 12:04:24 -0800 | [diff] [blame] | 778 | } |
| 779 | |
| 780 | void rsdAllocationIoReceive(const Context *rsc, Allocation *alloc) { |
Jason Sams | 93eacc7 | 2012-12-18 14:26:57 -0800 | [diff] [blame] | 781 | #ifndef RS_COMPATIBILITY_LIB |
Jason Sams | 3522f40 | 2012-03-23 11:47:26 -0700 | [diff] [blame] | 782 | DrvAllocation *drv = (DrvAllocation *)alloc->mHal.drv; |
Jason Sams | 733396b | 2013-02-22 12:46:18 -0800 | [diff] [blame] | 783 | |
| 784 | if (alloc->mHal.state.usageFlags & RS_ALLOCATION_USAGE_SCRIPT) { |
Eino-Ville Talvala | 0d44f12 | 2013-04-03 16:42:35 -0700 | [diff] [blame] | 785 | CpuConsumer::LockedBuffer lb; |
| 786 | status_t ret = drv->cpuConsumer->lockNextBuffer(&lb); |
| 787 | if (ret == OK) { |
| 788 | if (drv->lb.data != NULL) { |
| 789 | drv->cpuConsumer->unlockBuffer(drv->lb); |
| 790 | } |
| 791 | drv->lb = lb; |
| 792 | alloc->mHal.drvState.lod[0].mallocPtr = drv->lb.data; |
| 793 | alloc->mHal.drvState.lod[0].stride = drv->lb.stride * |
| 794 | alloc->mHal.state.elementSizeBytes; |
Jason Sams | 733396b | 2013-02-22 12:46:18 -0800 | [diff] [blame] | 795 | |
Eino-Ville Talvala | 0d44f12 | 2013-04-03 16:42:35 -0700 | [diff] [blame] | 796 | if (alloc->mHal.state.yuv) { |
| 797 | DeriveYUVLayout(alloc->mHal.state.yuv, &alloc->mHal.drvState); |
| 798 | } |
| 799 | } else if (ret == BAD_VALUE) { |
| 800 | // No new frame, don't do anything |
| 801 | } else { |
| 802 | rsc->setError(RS_ERROR_DRIVER, "Error receiving IO input buffer."); |
Jason Sams | 733396b | 2013-02-22 12:46:18 -0800 | [diff] [blame] | 803 | } |
| 804 | |
| 805 | } else { |
Tim Murray | 3a25fdd | 2013-02-22 17:56:56 -0800 | [diff] [blame] | 806 | drv->surfaceTexture->updateTexImage(); |
Jason Sams | 733396b | 2013-02-22 12:46:18 -0800 | [diff] [blame] | 807 | } |
| 808 | |
| 809 | |
Jason Sams | 93eacc7 | 2012-12-18 14:26:57 -0800 | [diff] [blame] | 810 | #endif |
Jason Sams | 7ac2a4d | 2012-02-15 12:04:24 -0800 | [diff] [blame] | 811 | } |
| 812 | |
| 813 | |
Jason Sams | eb4fe18 | 2011-05-26 16:33:01 -0700 | [diff] [blame] | 814 | void rsdAllocationData1D(const Context *rsc, const Allocation *alloc, |
Tim Murray | 099bc26 | 2013-03-20 16:54:03 -0700 | [diff] [blame] | 815 | uint32_t xoff, uint32_t lod, size_t count, |
Alex Sakhartchouk | c794cd5 | 2012-02-13 11:57:32 -0800 | [diff] [blame] | 816 | const void *data, size_t sizeBytes) { |
Jason Sams | eb4fe18 | 2011-05-26 16:33:01 -0700 | [diff] [blame] | 817 | DrvAllocation *drv = (DrvAllocation *)alloc->mHal.drv; |
| 818 | |
Tim Murray | 099bc26 | 2013-03-20 16:54:03 -0700 | [diff] [blame] | 819 | const size_t eSize = alloc->mHal.state.type->getElementSizeBytes(); |
Jason Sams | 3bbc0fd | 2013-04-09 14:16:13 -0700 | [diff] [blame] | 820 | uint8_t * ptr = GetOffsetPtr(alloc, xoff, 0, 0, 0, RS_ALLOCATION_CUBEMAP_FACE_POSITIVE_X); |
Tim Murray | 099bc26 | 2013-03-20 16:54:03 -0700 | [diff] [blame] | 821 | size_t size = count * eSize; |
Jason Sams | eb4fe18 | 2011-05-26 16:33:01 -0700 | [diff] [blame] | 822 | |
Stephen Hines | 20c535f | 2012-12-06 19:04:38 -0800 | [diff] [blame] | 823 | if (ptr != data) { |
| 824 | // Skip the copy if we are the same allocation. This can arise from |
| 825 | // our Bitmap optimization, where we share the same storage. |
| 826 | if (alloc->mHal.state.hasReferences) { |
| 827 | alloc->incRefs(data, count); |
| 828 | alloc->decRefs(ptr, count); |
| 829 | } |
| 830 | memcpy(ptr, data, size); |
Jason Sams | eb4fe18 | 2011-05-26 16:33:01 -0700 | [diff] [blame] | 831 | } |
Jason Sams | eb4fe18 | 2011-05-26 16:33:01 -0700 | [diff] [blame] | 832 | drv->uploadDeferred = true; |
| 833 | } |
| 834 | |
| 835 | void rsdAllocationData2D(const Context *rsc, const Allocation *alloc, |
| 836 | uint32_t xoff, uint32_t yoff, uint32_t lod, RsAllocationCubemapFace face, |
Tim Murray | 358747a | 2012-11-26 13:52:04 -0800 | [diff] [blame] | 837 | 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] | 838 | DrvAllocation *drv = (DrvAllocation *)alloc->mHal.drv; |
| 839 | |
Tim Murray | 099bc26 | 2013-03-20 16:54:03 -0700 | [diff] [blame] | 840 | size_t eSize = alloc->mHal.state.elementSizeBytes; |
| 841 | size_t lineSize = eSize * w; |
Tim Murray | 358747a | 2012-11-26 13:52:04 -0800 | [diff] [blame] | 842 | if (!stride) { |
| 843 | stride = lineSize; |
| 844 | } |
Jason Sams | eb4fe18 | 2011-05-26 16:33:01 -0700 | [diff] [blame] | 845 | |
Jason Sams | 709a097 | 2012-11-15 18:18:04 -0800 | [diff] [blame] | 846 | if (alloc->mHal.drvState.lod[0].mallocPtr) { |
Jason Sams | eb4fe18 | 2011-05-26 16:33:01 -0700 | [diff] [blame] | 847 | const uint8_t *src = static_cast<const uint8_t *>(data); |
Jason Sams | 3bbc0fd | 2013-04-09 14:16:13 -0700 | [diff] [blame] | 848 | uint8_t *dst = GetOffsetPtr(alloc, xoff, yoff, 0, lod, face); |
Stephen Hines | 20c535f | 2012-12-06 19:04:38 -0800 | [diff] [blame] | 849 | if (dst == src) { |
| 850 | // Skip the copy if we are the same allocation. This can arise from |
| 851 | // our Bitmap optimization, where we share the same storage. |
| 852 | drv->uploadDeferred = true; |
| 853 | return; |
| 854 | } |
Jason Sams | eb4fe18 | 2011-05-26 16:33:01 -0700 | [diff] [blame] | 855 | |
| 856 | for (uint32_t line=yoff; line < (yoff+h); line++) { |
| 857 | if (alloc->mHal.state.hasReferences) { |
| 858 | alloc->incRefs(src, w); |
| 859 | alloc->decRefs(dst, w); |
| 860 | } |
| 861 | memcpy(dst, src, lineSize); |
Tim Murray | 358747a | 2012-11-26 13:52:04 -0800 | [diff] [blame] | 862 | src += stride; |
Jason Sams | 709a097 | 2012-11-15 18:18:04 -0800 | [diff] [blame] | 863 | dst += alloc->mHal.drvState.lod[lod].stride; |
Jason Sams | eb4fe18 | 2011-05-26 16:33:01 -0700 | [diff] [blame] | 864 | } |
Jason Sams | bc0ca6b | 2013-02-15 18:13:43 -0800 | [diff] [blame] | 865 | if (alloc->mHal.state.yuv) { |
| 866 | int lod = 1; |
| 867 | while (alloc->mHal.drvState.lod[lod].mallocPtr) { |
Tim Murray | 099bc26 | 2013-03-20 16:54:03 -0700 | [diff] [blame] | 868 | size_t lineSize = alloc->mHal.drvState.lod[lod].dimX; |
Jason Sams | 3bbc0fd | 2013-04-09 14:16:13 -0700 | [diff] [blame] | 869 | uint8_t *dst = GetOffsetPtr(alloc, xoff, yoff, 0, lod, face); |
Jason Sams | bc0ca6b | 2013-02-15 18:13:43 -0800 | [diff] [blame] | 870 | |
| 871 | for (uint32_t line=(yoff >> 1); line < ((yoff+h)>>1); line++) { |
| 872 | memcpy(dst, src, lineSize); |
| 873 | src += lineSize; |
| 874 | dst += alloc->mHal.drvState.lod[lod].stride; |
| 875 | } |
| 876 | lod++; |
| 877 | } |
| 878 | |
| 879 | } |
Jason Sams | eb4fe18 | 2011-05-26 16:33:01 -0700 | [diff] [blame] | 880 | drv->uploadDeferred = true; |
| 881 | } else { |
Jason Sams | 2382aba | 2011-09-13 15:41:01 -0700 | [diff] [blame] | 882 | Update2DTexture(rsc, alloc, data, xoff, yoff, lod, face, w, h); |
Jason Sams | eb4fe18 | 2011-05-26 16:33:01 -0700 | [diff] [blame] | 883 | } |
| 884 | } |
| 885 | |
| 886 | void rsdAllocationData3D(const Context *rsc, const Allocation *alloc, |
| 887 | uint32_t xoff, uint32_t yoff, uint32_t zoff, |
Jason Sams | 3bbc0fd | 2013-04-09 14:16:13 -0700 | [diff] [blame] | 888 | uint32_t lod, |
| 889 | uint32_t w, uint32_t h, uint32_t d, const void *data, |
| 890 | size_t sizeBytes, size_t stride) { |
| 891 | DrvAllocation *drv = (DrvAllocation *)alloc->mHal.drv; |
Jason Sams | eb4fe18 | 2011-05-26 16:33:01 -0700 | [diff] [blame] | 892 | |
Jason Sams | 3bbc0fd | 2013-04-09 14:16:13 -0700 | [diff] [blame] | 893 | uint32_t eSize = alloc->mHal.state.elementSizeBytes; |
| 894 | uint32_t lineSize = eSize * w; |
| 895 | if (!stride) { |
| 896 | stride = lineSize; |
| 897 | } |
| 898 | |
| 899 | if (alloc->mHal.drvState.lod[0].mallocPtr) { |
| 900 | const uint8_t *src = static_cast<const uint8_t *>(data); |
| 901 | for (uint32_t z = zoff; z < d; z++) { |
| 902 | uint8_t *dst = GetOffsetPtr(alloc, xoff, yoff, z, lod, |
| 903 | RS_ALLOCATION_CUBEMAP_FACE_POSITIVE_X); |
| 904 | if (dst == src) { |
| 905 | // Skip the copy if we are the same allocation. This can arise from |
| 906 | // our Bitmap optimization, where we share the same storage. |
| 907 | drv->uploadDeferred = true; |
| 908 | return; |
| 909 | } |
| 910 | |
| 911 | for (uint32_t line=yoff; line < (yoff+h); line++) { |
| 912 | if (alloc->mHal.state.hasReferences) { |
| 913 | alloc->incRefs(src, w); |
| 914 | alloc->decRefs(dst, w); |
| 915 | } |
| 916 | memcpy(dst, src, lineSize); |
| 917 | src += stride; |
| 918 | dst += alloc->mHal.drvState.lod[lod].stride; |
| 919 | } |
| 920 | } |
| 921 | drv->uploadDeferred = true; |
| 922 | } |
Jason Sams | eb4fe18 | 2011-05-26 16:33:01 -0700 | [diff] [blame] | 923 | } |
| 924 | |
Jason Sams | 807fdc4 | 2012-07-25 17:55:39 -0700 | [diff] [blame] | 925 | void rsdAllocationRead1D(const Context *rsc, const Allocation *alloc, |
Tim Murray | 099bc26 | 2013-03-20 16:54:03 -0700 | [diff] [blame] | 926 | uint32_t xoff, uint32_t lod, size_t count, |
Jason Sams | 807fdc4 | 2012-07-25 17:55:39 -0700 | [diff] [blame] | 927 | void *data, size_t sizeBytes) { |
Tim Murray | 099bc26 | 2013-03-20 16:54:03 -0700 | [diff] [blame] | 928 | const size_t eSize = alloc->mHal.state.type->getElementSizeBytes(); |
Jason Sams | 3bbc0fd | 2013-04-09 14:16:13 -0700 | [diff] [blame] | 929 | const uint8_t * ptr = GetOffsetPtr(alloc, xoff, 0, 0, 0, RS_ALLOCATION_CUBEMAP_FACE_POSITIVE_X); |
Stephen Hines | 20c535f | 2012-12-06 19:04:38 -0800 | [diff] [blame] | 930 | if (data != ptr) { |
| 931 | // Skip the copy if we are the same allocation. This can arise from |
| 932 | // our Bitmap optimization, where we share the same storage. |
| 933 | memcpy(data, ptr, count * eSize); |
| 934 | } |
Jason Sams | 807fdc4 | 2012-07-25 17:55:39 -0700 | [diff] [blame] | 935 | } |
| 936 | |
| 937 | void rsdAllocationRead2D(const Context *rsc, const Allocation *alloc, |
Tim Murray | 358747a | 2012-11-26 13:52:04 -0800 | [diff] [blame] | 938 | uint32_t xoff, uint32_t yoff, uint32_t lod, RsAllocationCubemapFace face, |
| 939 | uint32_t w, uint32_t h, void *data, size_t sizeBytes, size_t stride) { |
Tim Murray | 099bc26 | 2013-03-20 16:54:03 -0700 | [diff] [blame] | 940 | size_t eSize = alloc->mHal.state.elementSizeBytes; |
| 941 | size_t lineSize = eSize * w; |
Tim Murray | 358747a | 2012-11-26 13:52:04 -0800 | [diff] [blame] | 942 | if (!stride) { |
| 943 | stride = lineSize; |
| 944 | } |
Jason Sams | 807fdc4 | 2012-07-25 17:55:39 -0700 | [diff] [blame] | 945 | |
Jason Sams | 709a097 | 2012-11-15 18:18:04 -0800 | [diff] [blame] | 946 | if (alloc->mHal.drvState.lod[0].mallocPtr) { |
Jason Sams | 807fdc4 | 2012-07-25 17:55:39 -0700 | [diff] [blame] | 947 | uint8_t *dst = static_cast<uint8_t *>(data); |
Jason Sams | 3bbc0fd | 2013-04-09 14:16:13 -0700 | [diff] [blame] | 948 | const uint8_t *src = GetOffsetPtr(alloc, xoff, yoff, 0, lod, face); |
Stephen Hines | 20c535f | 2012-12-06 19:04:38 -0800 | [diff] [blame] | 949 | if (dst == src) { |
| 950 | // Skip the copy if we are the same allocation. This can arise from |
| 951 | // our Bitmap optimization, where we share the same storage. |
| 952 | return; |
| 953 | } |
Jason Sams | 807fdc4 | 2012-07-25 17:55:39 -0700 | [diff] [blame] | 954 | |
| 955 | for (uint32_t line=yoff; line < (yoff+h); line++) { |
| 956 | memcpy(dst, src, lineSize); |
Tim Murray | 358747a | 2012-11-26 13:52:04 -0800 | [diff] [blame] | 957 | dst += stride; |
Jason Sams | 709a097 | 2012-11-15 18:18:04 -0800 | [diff] [blame] | 958 | src += alloc->mHal.drvState.lod[lod].stride; |
Jason Sams | 807fdc4 | 2012-07-25 17:55:39 -0700 | [diff] [blame] | 959 | } |
| 960 | } else { |
| 961 | ALOGE("Add code to readback from non-script memory"); |
| 962 | } |
| 963 | } |
| 964 | |
Tim Murray | 358747a | 2012-11-26 13:52:04 -0800 | [diff] [blame] | 965 | |
Jason Sams | 807fdc4 | 2012-07-25 17:55:39 -0700 | [diff] [blame] | 966 | void rsdAllocationRead3D(const Context *rsc, const Allocation *alloc, |
| 967 | uint32_t xoff, uint32_t yoff, uint32_t zoff, |
Jason Sams | 3bbc0fd | 2013-04-09 14:16:13 -0700 | [diff] [blame] | 968 | uint32_t lod, |
| 969 | uint32_t w, uint32_t h, uint32_t d, void *data, size_t sizeBytes, size_t stride) { |
| 970 | uint32_t eSize = alloc->mHal.state.elementSizeBytes; |
| 971 | uint32_t lineSize = eSize * w; |
| 972 | if (!stride) { |
| 973 | stride = lineSize; |
| 974 | } |
Jason Sams | 807fdc4 | 2012-07-25 17:55:39 -0700 | [diff] [blame] | 975 | |
Jason Sams | 3bbc0fd | 2013-04-09 14:16:13 -0700 | [diff] [blame] | 976 | if (alloc->mHal.drvState.lod[0].mallocPtr) { |
| 977 | uint8_t *dst = static_cast<uint8_t *>(data); |
| 978 | for (uint32_t z = zoff; z < d; z++) { |
| 979 | const uint8_t *src = GetOffsetPtr(alloc, xoff, yoff, z, lod, |
| 980 | RS_ALLOCATION_CUBEMAP_FACE_POSITIVE_X); |
| 981 | if (dst == src) { |
| 982 | // Skip the copy if we are the same allocation. This can arise from |
| 983 | // our Bitmap optimization, where we share the same storage. |
| 984 | return; |
| 985 | } |
| 986 | |
| 987 | for (uint32_t line=yoff; line < (yoff+h); line++) { |
| 988 | memcpy(dst, src, lineSize); |
| 989 | dst += stride; |
| 990 | src += alloc->mHal.drvState.lod[lod].stride; |
| 991 | } |
| 992 | } |
| 993 | } |
Jason Sams | 807fdc4 | 2012-07-25 17:55:39 -0700 | [diff] [blame] | 994 | } |
| 995 | |
| 996 | void * rsdAllocationLock1D(const android::renderscript::Context *rsc, |
| 997 | const android::renderscript::Allocation *alloc) { |
Jason Sams | 709a097 | 2012-11-15 18:18:04 -0800 | [diff] [blame] | 998 | return alloc->mHal.drvState.lod[0].mallocPtr; |
Jason Sams | 807fdc4 | 2012-07-25 17:55:39 -0700 | [diff] [blame] | 999 | } |
| 1000 | |
| 1001 | void rsdAllocationUnlock1D(const android::renderscript::Context *rsc, |
| 1002 | const android::renderscript::Allocation *alloc) { |
| 1003 | |
| 1004 | } |
| 1005 | |
Alex Sakhartchouk | 74a8279 | 2011-06-14 11:13:19 -0700 | [diff] [blame] | 1006 | void rsdAllocationData1D_alloc(const android::renderscript::Context *rsc, |
| 1007 | const android::renderscript::Allocation *dstAlloc, |
Tim Murray | 099bc26 | 2013-03-20 16:54:03 -0700 | [diff] [blame] | 1008 | uint32_t dstXoff, uint32_t dstLod, size_t count, |
Alex Sakhartchouk | 74a8279 | 2011-06-14 11:13:19 -0700 | [diff] [blame] | 1009 | const android::renderscript::Allocation *srcAlloc, |
Alex Sakhartchouk | a949524 | 2011-06-16 11:05:13 -0700 | [diff] [blame] | 1010 | uint32_t srcXoff, uint32_t srcLod) { |
| 1011 | } |
| 1012 | |
Alex Sakhartchouk | a949524 | 2011-06-16 11:05:13 -0700 | [diff] [blame] | 1013 | |
| 1014 | void rsdAllocationData2D_alloc_script(const android::renderscript::Context *rsc, |
| 1015 | const android::renderscript::Allocation *dstAlloc, |
| 1016 | uint32_t dstXoff, uint32_t dstYoff, uint32_t dstLod, |
| 1017 | RsAllocationCubemapFace dstFace, uint32_t w, uint32_t h, |
| 1018 | const android::renderscript::Allocation *srcAlloc, |
| 1019 | uint32_t srcXoff, uint32_t srcYoff, uint32_t srcLod, |
| 1020 | RsAllocationCubemapFace srcFace) { |
Tim Murray | 099bc26 | 2013-03-20 16:54:03 -0700 | [diff] [blame] | 1021 | size_t elementSize = dstAlloc->getType()->getElementSizeBytes(); |
Alex Sakhartchouk | a949524 | 2011-06-16 11:05:13 -0700 | [diff] [blame] | 1022 | for (uint32_t i = 0; i < h; i ++) { |
Jason Sams | 3bbc0fd | 2013-04-09 14:16:13 -0700 | [diff] [blame] | 1023 | uint8_t *dstPtr = GetOffsetPtr(dstAlloc, dstXoff, dstYoff + i, 0, dstLod, dstFace); |
| 1024 | uint8_t *srcPtr = GetOffsetPtr(srcAlloc, srcXoff, srcYoff + i, 0, srcLod, srcFace); |
Alex Sakhartchouk | a949524 | 2011-06-16 11:05:13 -0700 | [diff] [blame] | 1025 | memcpy(dstPtr, srcPtr, w * elementSize); |
| 1026 | |
Steve Block | af12ac6 | 2012-01-06 19:20:56 +0000 | [diff] [blame] | 1027 | //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] | 1028 | // dstXoff, dstYoff, dstLod, dstFace, w, h, srcXoff, srcYoff, srcLod, srcFace); |
Alex Sakhartchouk | a949524 | 2011-06-16 11:05:13 -0700 | [diff] [blame] | 1029 | } |
Alex Sakhartchouk | 74a8279 | 2011-06-14 11:13:19 -0700 | [diff] [blame] | 1030 | } |
| 1031 | |
Jason Sams | 3bbc0fd | 2013-04-09 14:16:13 -0700 | [diff] [blame] | 1032 | void rsdAllocationData3D_alloc_script(const android::renderscript::Context *rsc, |
| 1033 | const android::renderscript::Allocation *dstAlloc, |
| 1034 | uint32_t dstXoff, uint32_t dstYoff, uint32_t dstZoff, uint32_t dstLod, |
| 1035 | uint32_t w, uint32_t h, uint32_t d, |
| 1036 | const android::renderscript::Allocation *srcAlloc, |
| 1037 | uint32_t srcXoff, uint32_t srcYoff, uint32_t srcZoff, uint32_t srcLod) { |
| 1038 | uint32_t elementSize = dstAlloc->getType()->getElementSizeBytes(); |
| 1039 | for (uint32_t j = 0; j < d; j++) { |
| 1040 | for (uint32_t i = 0; i < h; i ++) { |
| 1041 | uint8_t *dstPtr = GetOffsetPtr(dstAlloc, dstXoff, dstYoff + i, dstZoff + j, |
| 1042 | dstLod, RS_ALLOCATION_CUBEMAP_FACE_POSITIVE_X); |
| 1043 | uint8_t *srcPtr = GetOffsetPtr(srcAlloc, srcXoff, srcYoff + i, srcZoff + j, |
| 1044 | srcLod, RS_ALLOCATION_CUBEMAP_FACE_POSITIVE_X); |
| 1045 | memcpy(dstPtr, srcPtr, w * elementSize); |
| 1046 | |
| 1047 | //ALOGE("COPIED dstXoff(%u), dstYoff(%u), dstLod(%u), dstFace(%u), w(%u), h(%u), srcXoff(%u), srcYoff(%u), srcLod(%u), srcFace(%u)", |
| 1048 | // dstXoff, dstYoff, dstLod, dstFace, w, h, srcXoff, srcYoff, srcLod, srcFace); |
| 1049 | } |
| 1050 | } |
| 1051 | } |
| 1052 | |
Alex Sakhartchouk | 74a8279 | 2011-06-14 11:13:19 -0700 | [diff] [blame] | 1053 | void rsdAllocationData2D_alloc(const android::renderscript::Context *rsc, |
| 1054 | const android::renderscript::Allocation *dstAlloc, |
| 1055 | uint32_t dstXoff, uint32_t dstYoff, uint32_t dstLod, |
| 1056 | RsAllocationCubemapFace dstFace, uint32_t w, uint32_t h, |
| 1057 | const android::renderscript::Allocation *srcAlloc, |
| 1058 | uint32_t srcXoff, uint32_t srcYoff, uint32_t srcLod, |
| 1059 | RsAllocationCubemapFace srcFace) { |
Alex Sakhartchouk | a949524 | 2011-06-16 11:05:13 -0700 | [diff] [blame] | 1060 | if (!dstAlloc->getIsScript() && !srcAlloc->getIsScript()) { |
| 1061 | rsc->setError(RS_ERROR_FATAL_DRIVER, "Non-script allocation copies not " |
| 1062 | "yet implemented."); |
| 1063 | return; |
| 1064 | } |
| 1065 | rsdAllocationData2D_alloc_script(rsc, dstAlloc, dstXoff, dstYoff, |
| 1066 | dstLod, dstFace, w, h, srcAlloc, |
| 1067 | srcXoff, srcYoff, srcLod, srcFace); |
Alex Sakhartchouk | 74a8279 | 2011-06-14 11:13:19 -0700 | [diff] [blame] | 1068 | } |
| 1069 | |
| 1070 | void rsdAllocationData3D_alloc(const android::renderscript::Context *rsc, |
| 1071 | const android::renderscript::Allocation *dstAlloc, |
| 1072 | uint32_t dstXoff, uint32_t dstYoff, uint32_t dstZoff, |
Jason Sams | 3bbc0fd | 2013-04-09 14:16:13 -0700 | [diff] [blame] | 1073 | uint32_t dstLod, |
Alex Sakhartchouk | 74a8279 | 2011-06-14 11:13:19 -0700 | [diff] [blame] | 1074 | uint32_t w, uint32_t h, uint32_t d, |
| 1075 | const android::renderscript::Allocation *srcAlloc, |
| 1076 | uint32_t srcXoff, uint32_t srcYoff, uint32_t srcZoff, |
Jason Sams | 3bbc0fd | 2013-04-09 14:16:13 -0700 | [diff] [blame] | 1077 | uint32_t srcLod) { |
| 1078 | if (!dstAlloc->getIsScript() && !srcAlloc->getIsScript()) { |
| 1079 | rsc->setError(RS_ERROR_FATAL_DRIVER, "Non-script allocation copies not " |
| 1080 | "yet implemented."); |
| 1081 | return; |
| 1082 | } |
| 1083 | rsdAllocationData3D_alloc_script(rsc, dstAlloc, dstXoff, dstYoff, dstZoff, |
| 1084 | dstLod, w, h, d, srcAlloc, |
| 1085 | srcXoff, srcYoff, srcZoff, srcLod); |
Alex Sakhartchouk | 74a8279 | 2011-06-14 11:13:19 -0700 | [diff] [blame] | 1086 | } |
| 1087 | |
Jason Sams | eb4fe18 | 2011-05-26 16:33:01 -0700 | [diff] [blame] | 1088 | void rsdAllocationElementData1D(const Context *rsc, const Allocation *alloc, |
| 1089 | uint32_t x, |
Tim Murray | 099bc26 | 2013-03-20 16:54:03 -0700 | [diff] [blame] | 1090 | const void *data, uint32_t cIdx, size_t sizeBytes) { |
Jason Sams | eb4fe18 | 2011-05-26 16:33:01 -0700 | [diff] [blame] | 1091 | DrvAllocation *drv = (DrvAllocation *)alloc->mHal.drv; |
| 1092 | |
Tim Murray | 099bc26 | 2013-03-20 16:54:03 -0700 | [diff] [blame] | 1093 | size_t eSize = alloc->mHal.state.elementSizeBytes; |
Jason Sams | 3bbc0fd | 2013-04-09 14:16:13 -0700 | [diff] [blame] | 1094 | uint8_t * ptr = GetOffsetPtr(alloc, x, 0, 0, 0, RS_ALLOCATION_CUBEMAP_FACE_POSITIVE_X); |
Jason Sams | eb4fe18 | 2011-05-26 16:33:01 -0700 | [diff] [blame] | 1095 | |
| 1096 | const Element * e = alloc->mHal.state.type->getElement()->getField(cIdx); |
| 1097 | ptr += alloc->mHal.state.type->getElement()->getFieldOffsetBytes(cIdx); |
| 1098 | |
| 1099 | if (alloc->mHal.state.hasReferences) { |
| 1100 | e->incRefs(data); |
| 1101 | e->decRefs(ptr); |
| 1102 | } |
| 1103 | |
| 1104 | memcpy(ptr, data, sizeBytes); |
| 1105 | drv->uploadDeferred = true; |
| 1106 | } |
| 1107 | |
| 1108 | void rsdAllocationElementData2D(const Context *rsc, const Allocation *alloc, |
| 1109 | uint32_t x, uint32_t y, |
Tim Murray | 099bc26 | 2013-03-20 16:54:03 -0700 | [diff] [blame] | 1110 | const void *data, uint32_t cIdx, size_t sizeBytes) { |
Jason Sams | eb4fe18 | 2011-05-26 16:33:01 -0700 | [diff] [blame] | 1111 | DrvAllocation *drv = (DrvAllocation *)alloc->mHal.drv; |
| 1112 | |
Tim Murray | 099bc26 | 2013-03-20 16:54:03 -0700 | [diff] [blame] | 1113 | size_t eSize = alloc->mHal.state.elementSizeBytes; |
Jason Sams | 3bbc0fd | 2013-04-09 14:16:13 -0700 | [diff] [blame] | 1114 | uint8_t * ptr = GetOffsetPtr(alloc, x, y, 0, 0, RS_ALLOCATION_CUBEMAP_FACE_POSITIVE_X); |
Jason Sams | eb4fe18 | 2011-05-26 16:33:01 -0700 | [diff] [blame] | 1115 | |
| 1116 | const Element * e = alloc->mHal.state.type->getElement()->getField(cIdx); |
| 1117 | ptr += alloc->mHal.state.type->getElement()->getFieldOffsetBytes(cIdx); |
| 1118 | |
| 1119 | if (alloc->mHal.state.hasReferences) { |
| 1120 | e->incRefs(data); |
| 1121 | e->decRefs(ptr); |
| 1122 | } |
| 1123 | |
| 1124 | memcpy(ptr, data, sizeBytes); |
| 1125 | drv->uploadDeferred = true; |
| 1126 | } |
| 1127 | |
Jason Sams | 61a4bb7 | 2012-07-25 19:33:43 -0700 | [diff] [blame] | 1128 | static void mip565(const Allocation *alloc, int lod, RsAllocationCubemapFace face) { |
Jason Sams | 709a097 | 2012-11-15 18:18:04 -0800 | [diff] [blame] | 1129 | uint32_t w = alloc->mHal.drvState.lod[lod + 1].dimX; |
| 1130 | uint32_t h = alloc->mHal.drvState.lod[lod + 1].dimY; |
Jason Sams | 61a4bb7 | 2012-07-25 19:33:43 -0700 | [diff] [blame] | 1131 | |
| 1132 | for (uint32_t y=0; y < h; y++) { |
Jason Sams | 3bbc0fd | 2013-04-09 14:16:13 -0700 | [diff] [blame] | 1133 | uint16_t *oPtr = (uint16_t *)GetOffsetPtr(alloc, 0, y, 0, lod + 1, face); |
| 1134 | const uint16_t *i1 = (uint16_t *)GetOffsetPtr(alloc, 0, 0, y*2, lod, face); |
| 1135 | const uint16_t *i2 = (uint16_t *)GetOffsetPtr(alloc, 0, 0, y*2+1, lod, face); |
Jason Sams | 61a4bb7 | 2012-07-25 19:33:43 -0700 | [diff] [blame] | 1136 | |
| 1137 | for (uint32_t x=0; x < w; x++) { |
| 1138 | *oPtr = rsBoxFilter565(i1[0], i1[1], i2[0], i2[1]); |
| 1139 | oPtr ++; |
| 1140 | i1 += 2; |
| 1141 | i2 += 2; |
| 1142 | } |
| 1143 | } |
| 1144 | } |
| 1145 | |
| 1146 | static void mip8888(const Allocation *alloc, int lod, RsAllocationCubemapFace face) { |
Jason Sams | 709a097 | 2012-11-15 18:18:04 -0800 | [diff] [blame] | 1147 | uint32_t w = alloc->mHal.drvState.lod[lod + 1].dimX; |
| 1148 | uint32_t h = alloc->mHal.drvState.lod[lod + 1].dimY; |
Jason Sams | 61a4bb7 | 2012-07-25 19:33:43 -0700 | [diff] [blame] | 1149 | |
| 1150 | for (uint32_t y=0; y < h; y++) { |
Jason Sams | 3bbc0fd | 2013-04-09 14:16:13 -0700 | [diff] [blame] | 1151 | uint32_t *oPtr = (uint32_t *)GetOffsetPtr(alloc, 0, y, 0, lod + 1, face); |
| 1152 | const uint32_t *i1 = (uint32_t *)GetOffsetPtr(alloc, 0, y*2, 0, lod, face); |
| 1153 | const uint32_t *i2 = (uint32_t *)GetOffsetPtr(alloc, 0, y*2+1, 0, lod, face); |
Jason Sams | 61a4bb7 | 2012-07-25 19:33:43 -0700 | [diff] [blame] | 1154 | |
| 1155 | for (uint32_t x=0; x < w; x++) { |
| 1156 | *oPtr = rsBoxFilter8888(i1[0], i1[1], i2[0], i2[1]); |
| 1157 | oPtr ++; |
| 1158 | i1 += 2; |
| 1159 | i2 += 2; |
| 1160 | } |
| 1161 | } |
| 1162 | } |
| 1163 | |
| 1164 | static void mip8(const Allocation *alloc, int lod, RsAllocationCubemapFace face) { |
Jason Sams | 709a097 | 2012-11-15 18:18:04 -0800 | [diff] [blame] | 1165 | uint32_t w = alloc->mHal.drvState.lod[lod + 1].dimX; |
| 1166 | uint32_t h = alloc->mHal.drvState.lod[lod + 1].dimY; |
Jason Sams | 61a4bb7 | 2012-07-25 19:33:43 -0700 | [diff] [blame] | 1167 | |
| 1168 | for (uint32_t y=0; y < h; y++) { |
Jason Sams | 3bbc0fd | 2013-04-09 14:16:13 -0700 | [diff] [blame] | 1169 | uint8_t *oPtr = GetOffsetPtr(alloc, 0, y, 0, lod + 1, face); |
| 1170 | const uint8_t *i1 = GetOffsetPtr(alloc, 0, y*2, 0, lod, face); |
| 1171 | const uint8_t *i2 = GetOffsetPtr(alloc, 0, y*2+1, 0, lod, face); |
Jason Sams | 61a4bb7 | 2012-07-25 19:33:43 -0700 | [diff] [blame] | 1172 | |
| 1173 | for (uint32_t x=0; x < w; x++) { |
| 1174 | *oPtr = (uint8_t)(((uint32_t)i1[0] + i1[1] + i2[0] + i2[1]) * 0.25f); |
| 1175 | oPtr ++; |
| 1176 | i1 += 2; |
| 1177 | i2 += 2; |
| 1178 | } |
| 1179 | } |
| 1180 | } |
| 1181 | |
| 1182 | void rsdAllocationGenerateMipmaps(const Context *rsc, const Allocation *alloc) { |
Jason Sams | 709a097 | 2012-11-15 18:18:04 -0800 | [diff] [blame] | 1183 | if(!alloc->mHal.drvState.lod[0].mallocPtr) { |
Jason Sams | 61a4bb7 | 2012-07-25 19:33:43 -0700 | [diff] [blame] | 1184 | return; |
| 1185 | } |
| 1186 | uint32_t numFaces = alloc->getType()->getDimFaces() ? 6 : 1; |
| 1187 | for (uint32_t face = 0; face < numFaces; face ++) { |
| 1188 | for (uint32_t lod=0; lod < (alloc->getType()->getLODCount() -1); lod++) { |
| 1189 | switch (alloc->getType()->getElement()->getSizeBits()) { |
| 1190 | case 32: |
| 1191 | mip8888(alloc, lod, (RsAllocationCubemapFace)face); |
| 1192 | break; |
| 1193 | case 16: |
| 1194 | mip565(alloc, lod, (RsAllocationCubemapFace)face); |
| 1195 | break; |
| 1196 | case 8: |
| 1197 | mip8(alloc, lod, (RsAllocationCubemapFace)face); |
| 1198 | break; |
| 1199 | } |
| 1200 | } |
| 1201 | } |
| 1202 | } |