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