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