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