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