rsovAllocation.cpp revision 75f0d3110b04346b901771f96ce15cdbe907278f
1/* 2 * Copyright (C) 2016 The Android Open Source Project 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#include "rsovAllocation.h" 18 19#include <map> 20 21#include "rsAllocation.h" 22#include "rsContext.h" 23#include "rsCppUtils.h" 24#include "rsElement.h" 25#include "rsType.h" 26#include "rsovContext.h" 27#include "rsovCore.h" 28 29namespace android { 30namespace renderscript { 31namespace rsov { 32 33namespace { 34 35using std::make_pair; 36 37// TODO: handle 8-bit, 16-bit, and 64-bit integers and floating point numbers 38const std::map<std::pair<RsDataType, uint32_t>, VkFormat> mapElementToFormat{ 39 make_pair(make_pair(RS_TYPE_FLOAT_32, 1), VK_FORMAT_R32_SFLOAT), 40 make_pair(make_pair(RS_TYPE_FLOAT_32, 2), VK_FORMAT_R32G32_SFLOAT), 41 make_pair(make_pair(RS_TYPE_FLOAT_32, 3), VK_FORMAT_R32G32B32_SFLOAT), 42 make_pair(make_pair(RS_TYPE_FLOAT_32, 4), VK_FORMAT_R32G32B32A32_SFLOAT), 43 44 make_pair(make_pair(RS_TYPE_SIGNED_32, 1), VK_FORMAT_R32_SINT), 45 make_pair(make_pair(RS_TYPE_SIGNED_32, 2), VK_FORMAT_R32G32_SINT), 46 make_pair(make_pair(RS_TYPE_SIGNED_32, 3), VK_FORMAT_R32G32B32_SINT), 47 make_pair(make_pair(RS_TYPE_SIGNED_32, 4), VK_FORMAT_R32G32B32A32_SINT), 48 49 make_pair(make_pair(RS_TYPE_UNSIGNED_32, 1), VK_FORMAT_R32_UINT), 50 make_pair(make_pair(RS_TYPE_UNSIGNED_32, 2), VK_FORMAT_R32G32_UINT), 51 make_pair(make_pair(RS_TYPE_UNSIGNED_32, 3), VK_FORMAT_R32G32B32_UINT), 52 make_pair(make_pair(RS_TYPE_UNSIGNED_32, 4), VK_FORMAT_R32G32B32A32_UINT), 53}; 54 55VkFormat VkFormatFromRSElement(const Element &elem) { 56 // TODO: reject struct, allocation, and other non-numeric element 57 rsAssert(!elem.getFieldCount()); 58 59 RsDataType dataType = elem.getType(); 60 uint32_t vectorWidth = elem.getVectorSize(); 61 62 auto it = mapElementToFormat.find(make_pair(dataType, vectorWidth)); 63 if (it != mapElementToFormat.end()) { 64 return it->second; 65 } 66 67 rsAssert(0 && "Unexpected RS Element to map to VkFormat"); 68 69 return VK_FORMAT_R32G32B32A32_SFLOAT; 70} 71 72size_t DeriveYUVLayout(int yuv, Allocation::Hal::DrvState *state) { 73 // For the flexible YCbCr format, layout is initialized during call to 74 // Allocation::ioReceive. Return early and avoid clobberring any 75 // pre-existing layout. 76 if (yuv == HAL_PIXEL_FORMAT_YCbCr_420_888) { 77 return 0; 78 } 79 80 // YUV only supports basic 2d 81 // so we can stash the plane pointers in the mipmap levels. 82 size_t uvSize = 0; 83 state->lod[1].dimX = state->lod[0].dimX / 2; 84 state->lod[1].dimY = state->lod[0].dimY / 2; 85 state->lod[2].dimX = state->lod[0].dimX / 2; 86 state->lod[2].dimY = state->lod[0].dimY / 2; 87 state->yuv.shift = 1; 88 state->yuv.step = 1; 89 state->lodCount = 3; 90 91 switch (yuv) { 92 case HAL_PIXEL_FORMAT_YV12: 93 state->lod[2].stride = rsRound(state->lod[0].stride >> 1, 16); 94 state->lod[2].mallocPtr = ((uint8_t *)state->lod[0].mallocPtr) + 95 (state->lod[0].stride * state->lod[0].dimY); 96 uvSize += state->lod[2].stride * state->lod[2].dimY; 97 98 state->lod[1].stride = state->lod[2].stride; 99 state->lod[1].mallocPtr = ((uint8_t *)state->lod[2].mallocPtr) + 100 (state->lod[2].stride * state->lod[2].dimY); 101 uvSize += state->lod[1].stride * state->lod[2].dimY; 102 break; 103 case HAL_PIXEL_FORMAT_YCrCb_420_SP: // NV21 104 // state->lod[1].dimX = state->lod[0].dimX; 105 state->lod[1].stride = state->lod[0].stride; 106 state->lod[2].stride = state->lod[0].stride; 107 state->lod[2].mallocPtr = ((uint8_t *)state->lod[0].mallocPtr) + 108 (state->lod[0].stride * state->lod[0].dimY); 109 state->lod[1].mallocPtr = ((uint8_t *)state->lod[2].mallocPtr) + 1; 110 uvSize += state->lod[1].stride * state->lod[1].dimY; 111 state->yuv.step = 2; 112 break; 113 default: 114 rsAssert(0); 115 } 116 117 return uvSize; 118} 119 120// TODO: Dedup this with the same code under frameworks/rs/driver 121size_t AllocationBuildPointerTable(const Context *rsc, const Allocation *alloc, 122 const Type *type, uint8_t *ptr, 123 size_t requiredAlignment) { 124 alloc->mHal.drvState.lod[0].dimX = type->getDimX(); 125 alloc->mHal.drvState.lod[0].dimY = type->getDimY(); 126 alloc->mHal.drvState.lod[0].dimZ = type->getDimZ(); 127 alloc->mHal.drvState.lod[0].mallocPtr = 0; 128 // Stride needs to be aligned to a boundary defined by requiredAlignment! 129 size_t stride = 130 alloc->mHal.drvState.lod[0].dimX * type->getElementSizeBytes(); 131 alloc->mHal.drvState.lod[0].stride = rsRound(stride, requiredAlignment); 132 alloc->mHal.drvState.lodCount = type->getLODCount(); 133 alloc->mHal.drvState.faceCount = type->getDimFaces(); 134 135 size_t offsets[Allocation::MAX_LOD]; 136 memset(offsets, 0, sizeof(offsets)); 137 138 size_t o = alloc->mHal.drvState.lod[0].stride * 139 rsMax(alloc->mHal.drvState.lod[0].dimY, 1u) * 140 rsMax(alloc->mHal.drvState.lod[0].dimZ, 1u); 141 if (alloc->mHal.state.yuv) { 142 o += DeriveYUVLayout(alloc->mHal.state.yuv, &alloc->mHal.drvState); 143 144 for (uint32_t ct = 1; ct < alloc->mHal.drvState.lodCount; ct++) { 145 offsets[ct] = (size_t)alloc->mHal.drvState.lod[ct].mallocPtr; 146 } 147 } else if (alloc->mHal.drvState.lodCount > 1) { 148 uint32_t tx = alloc->mHal.drvState.lod[0].dimX; 149 uint32_t ty = alloc->mHal.drvState.lod[0].dimY; 150 uint32_t tz = alloc->mHal.drvState.lod[0].dimZ; 151 for (uint32_t lod = 1; lod < alloc->mHal.drvState.lodCount; lod++) { 152 alloc->mHal.drvState.lod[lod].dimX = tx; 153 alloc->mHal.drvState.lod[lod].dimY = ty; 154 alloc->mHal.drvState.lod[lod].dimZ = tz; 155 alloc->mHal.drvState.lod[lod].stride = 156 rsRound(tx * type->getElementSizeBytes(), requiredAlignment); 157 offsets[lod] = o; 158 o += alloc->mHal.drvState.lod[lod].stride * rsMax(ty, 1u) * rsMax(tz, 1u); 159 if (tx > 1) tx >>= 1; 160 if (ty > 1) ty >>= 1; 161 if (tz > 1) tz >>= 1; 162 } 163 } 164 165 alloc->mHal.drvState.faceOffset = o; 166 167 alloc->mHal.drvState.lod[0].mallocPtr = ptr; 168 for (uint32_t lod = 1; lod < alloc->mHal.drvState.lodCount; lod++) { 169 alloc->mHal.drvState.lod[lod].mallocPtr = ptr + offsets[lod]; 170 } 171 172 size_t allocSize = alloc->mHal.drvState.faceOffset; 173 if (alloc->mHal.drvState.faceCount) { 174 allocSize *= 6; 175 } 176 177 return allocSize; 178} 179 180size_t AllocationBuildPointerTable(const Context *rsc, const Allocation *alloc, 181 const Type *type, uint8_t *ptr) { 182 return AllocationBuildPointerTable(rsc, alloc, type, ptr, 183 Allocation::kMinimumRSAlignment); 184} 185 186uint8_t *GetOffsetPtr(const Allocation *alloc, uint32_t xoff, uint32_t yoff, 187 uint32_t zoff, uint32_t lod, 188 RsAllocationCubemapFace face) { 189 uint8_t *ptr = (uint8_t *)alloc->mHal.drvState.lod[lod].mallocPtr; 190 ptr += face * alloc->mHal.drvState.faceOffset; 191 ptr += zoff * alloc->mHal.drvState.lod[lod].dimY * 192 alloc->mHal.drvState.lod[lod].stride; 193 ptr += yoff * alloc->mHal.drvState.lod[lod].stride; 194 ptr += xoff * alloc->mHal.state.elementSizeBytes; 195 return ptr; 196} 197 198void mip565(const Allocation *alloc, int lod, RsAllocationCubemapFace face) { 199 uint32_t w = alloc->mHal.drvState.lod[lod + 1].dimX; 200 uint32_t h = alloc->mHal.drvState.lod[lod + 1].dimY; 201 202 for (uint32_t y = 0; y < h; y++) { 203 uint16_t *oPtr = (uint16_t *)GetOffsetPtr(alloc, 0, y, 0, lod + 1, face); 204 const uint16_t *i1 = 205 (uint16_t *)GetOffsetPtr(alloc, 0, 0, y * 2, lod, face); 206 const uint16_t *i2 = 207 (uint16_t *)GetOffsetPtr(alloc, 0, 0, y * 2 + 1, lod, face); 208 209 for (uint32_t x = 0; x < w; x++) { 210 *oPtr = rsBoxFilter565(i1[0], i1[1], i2[0], i2[1]); 211 oPtr++; 212 i1 += 2; 213 i2 += 2; 214 } 215 } 216} 217 218void mip8888(const Allocation *alloc, int lod, RsAllocationCubemapFace face) { 219 uint32_t w = alloc->mHal.drvState.lod[lod + 1].dimX; 220 uint32_t h = alloc->mHal.drvState.lod[lod + 1].dimY; 221 222 for (uint32_t y = 0; y < h; y++) { 223 uint32_t *oPtr = (uint32_t *)GetOffsetPtr(alloc, 0, y, 0, lod + 1, face); 224 const uint32_t *i1 = 225 (uint32_t *)GetOffsetPtr(alloc, 0, y * 2, 0, lod, face); 226 const uint32_t *i2 = 227 (uint32_t *)GetOffsetPtr(alloc, 0, y * 2 + 1, 0, lod, face); 228 229 for (uint32_t x = 0; x < w; x++) { 230 *oPtr = rsBoxFilter8888(i1[0], i1[1], i2[0], i2[1]); 231 oPtr++; 232 i1 += 2; 233 i2 += 2; 234 } 235 } 236} 237 238void mip8(const Allocation *alloc, int lod, RsAllocationCubemapFace face) { 239 uint32_t w = alloc->mHal.drvState.lod[lod + 1].dimX; 240 uint32_t h = alloc->mHal.drvState.lod[lod + 1].dimY; 241 242 for (uint32_t y = 0; y < h; y++) { 243 uint8_t *oPtr = GetOffsetPtr(alloc, 0, y, 0, lod + 1, face); 244 const uint8_t *i1 = GetOffsetPtr(alloc, 0, y * 2, 0, lod, face); 245 const uint8_t *i2 = GetOffsetPtr(alloc, 0, y * 2 + 1, 0, lod, face); 246 247 for (uint32_t x = 0; x < w; x++) { 248 *oPtr = (uint8_t)(((uint32_t)i1[0] + i1[1] + i2[0] + i2[1]) * 0.25f); 249 oPtr++; 250 i1 += 2; 251 i2 += 2; 252 } 253 } 254} 255 256} // anonymous namespace 257 258RSoVAllocation::RSoVAllocation(RSoVContext *context, const Type *type) 259 : mRSoV(context), 260 mDevice(context->getDevice()), 261 mType(type), 262 mWidth(type->getDimX()), 263 mHeight(type->getDimY()), 264 mDepth(type->getDimZ()) { 265 InitImage(); 266} 267 268RSoVAllocation::~RSoVAllocation() { 269 vkDestroyImageView(mDevice, mImageView, nullptr); 270 vkDestroyImage(mDevice, mImage, nullptr); 271 vkFreeMemory(mDevice, mMem, nullptr); 272} 273 274void RSoVAllocation::InitImage() { 275 VkResult res; 276 277 mFormat = VkFormatFromRSElement(*mType->getElement()); 278 279 const uint32_t width = mWidth; 280 const uint32_t height = mHeight; 281 const uint32_t depth = mDepth; 282 283 VkImageType imageType = 284 depth > 0 ? VK_IMAGE_TYPE_3D 285 : (height > 0 ? VK_IMAGE_TYPE_2D : VK_IMAGE_TYPE_1D); 286 287 VkImageCreateInfo createInfo = { 288 .sType = VK_STRUCTURE_TYPE_IMAGE_CREATE_INFO, 289 .pNext = nullptr, 290 .flags = 0, 291 .imageType = imageType, 292 .format = mFormat, 293 .extent = {width, rsMax(height, 1U), rsMax(depth, 1U)}, 294 .mipLevels = 1, 295 .arrayLayers = 1, 296 .samples = VK_SAMPLE_COUNT_1_BIT, 297 .tiling = VK_IMAGE_TILING_LINEAR, 298 .usage = VK_IMAGE_USAGE_STORAGE_BIT, 299 .sharingMode = VK_SHARING_MODE_EXCLUSIVE, 300 .queueFamilyIndexCount = 0, 301 .pQueueFamilyIndices = nullptr, 302 .initialLayout = VK_IMAGE_LAYOUT_PREINITIALIZED, 303 }; 304 305 res = vkCreateImage(mDevice, &createInfo, nullptr, &mImage); 306 rsAssert(res == VK_SUCCESS); 307 308 VkMemoryRequirements mem_reqs; 309 vkGetImageMemoryRequirements(mDevice, mImage, &mem_reqs); 310 311 ALOGI("size of memory needed = %u", (uint)mem_reqs.size); 312 313 VkMemoryAllocateInfo allocateInfo = { 314 .sType = VK_STRUCTURE_TYPE_MEMORY_ALLOCATE_INFO, 315 .pNext = nullptr, 316 .allocationSize = mem_reqs.size, 317 }; 318 319 /* Use the memory properties to determine the type of memory required */ 320 bool pass; 321 pass = mRSoV->MemoryTypeFromProperties( 322 mem_reqs.memoryTypeBits, VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT | 323 VK_MEMORY_PROPERTY_HOST_COHERENT_BIT, 324 &allocateInfo.memoryTypeIndex); 325 ALOGI("TypeBits = 0x%08X", mem_reqs.memoryTypeBits); 326 rsAssert(pass); 327 328 // TODO: Make this aligned 329 res = vkAllocateMemory(mDevice, &allocateInfo, nullptr, &mMem); 330 rsAssert(res == VK_SUCCESS); 331 332 res = vkBindImageMemory(mDevice, mImage, mMem, 0); 333 rsAssert(res == VK_SUCCESS); 334 335 VkImageViewType viewType = 336 depth > 0 ? VK_IMAGE_VIEW_TYPE_3D 337 : (height > 0 ? VK_IMAGE_VIEW_TYPE_2D : VK_IMAGE_VIEW_TYPE_1D); 338 339 VkImageViewCreateInfo view_info = { 340 .sType = VK_STRUCTURE_TYPE_IMAGE_VIEW_CREATE_INFO, 341 .pNext = nullptr, 342 .image = mImage, 343 .viewType = viewType, 344 .format = mFormat, 345 .components = 346 { 347 .r = VK_COMPONENT_SWIZZLE_IDENTITY, 348 .g = VK_COMPONENT_SWIZZLE_IDENTITY, 349 .b = VK_COMPONENT_SWIZZLE_IDENTITY, 350 .a = VK_COMPONENT_SWIZZLE_IDENTITY, 351 }, 352 .subresourceRange = 353 { 354 .aspectMask = VK_IMAGE_ASPECT_COLOR_BIT, 355 .baseMipLevel = 0, 356 .levelCount = 1, 357 .baseArrayLayer = 0, 358 .layerCount = 1, 359 }, 360 }; 361 362 res = vkCreateImageView(mDevice, &view_info, nullptr, &mImageView); 363 rsAssert(res == VK_SUCCESS); 364 365 mImageLayout = VK_IMAGE_LAYOUT_GENERAL; 366 367 mImageInfo = { 368 .imageView = mImageView, .imageLayout = mImageLayout, 369 }; 370 371 res = vkMapMemory(mDevice, mMem, 0, mem_reqs.size, 0, (void **)&mPtr); 372 rsAssert(res == VK_SUCCESS); 373} 374 375} // namespace rsov 376} // namespace renderscript 377} // namespace android 378 379using android::renderscript::Allocation; 380using android::renderscript::Context; 381using android::renderscript::Element; 382using android::renderscript::Type; 383using android::renderscript::rs_allocation; 384using android::renderscript::rsMax; 385using namespace android::renderscript::rsov; 386 387bool rsovAllocationInit(const Context *rsc, Allocation *alloc, bool forceZero) { 388 RSoVHal *hal = static_cast<RSoVHal *>(rsc->mHal.drv); 389 RSoVContext *rsov = hal->mRSoV; 390 const Type *type = alloc->getType(); 391 392 RSoVAllocation *rsovAlloc = new RSoVAllocation(rsov, type); 393 alloc->mHal.drv = rsovAlloc; 394 AllocationBuildPointerTable(rsc, alloc, type, 395 (uint8_t *)rsovAlloc->getHostPtr()); 396 return true; 397} 398 399void rsovAllocationDestroy(const Context *rsc, Allocation *alloc) { 400 RSoVAllocation *rsovAlloc = static_cast<RSoVAllocation *>(alloc->mHal.drv); 401 delete rsovAlloc; 402 alloc->mHal.drv = nullptr; 403} 404 405void rsovAllocationData1D(const Context *rsc, const Allocation *alloc, 406 uint32_t xoff, uint32_t lod, size_t count, 407 const void *data, size_t sizeBytes) { 408 const size_t eSize = alloc->mHal.state.type->getElementSizeBytes(); 409 uint8_t *ptr = 410 GetOffsetPtr(alloc, xoff, 0, 0, 0, RS_ALLOCATION_CUBEMAP_FACE_POSITIVE_X); 411 size_t size = count * eSize; 412 if (ptr != data) { 413 // Skip the copy if we are the same allocation. This can arise from 414 // our Bitmap optimization, where we share the same storage. 415 if (alloc->mHal.state.hasReferences) { 416 alloc->incRefs(data, count); 417 alloc->decRefs(ptr, count); 418 } 419 memcpy(ptr, data, size); 420 } 421} 422 423void rsovAllocationData2D(const Context *rsc, const Allocation *alloc, 424 uint32_t xoff, uint32_t yoff, uint32_t lod, 425 RsAllocationCubemapFace face, uint32_t w, uint32_t h, 426 const void *data, size_t sizeBytes, size_t stride) { 427 size_t eSize = alloc->mHal.state.elementSizeBytes; 428 size_t lineSize = eSize * w; 429 if (!stride) { 430 stride = lineSize; 431 } 432 433 if (alloc->mHal.drvState.lod[0].mallocPtr) { 434 const uint8_t *src = static_cast<const uint8_t *>(data); 435 uint8_t *dst = GetOffsetPtr(alloc, xoff, yoff, 0, lod, face); 436 437 for (uint32_t line = yoff; line < (yoff + h); line++) { 438 if (alloc->mHal.state.hasReferences) { 439 alloc->incRefs(src, w); 440 alloc->decRefs(dst, w); 441 } 442 memcpy(dst, src, lineSize); 443 src += stride; 444 dst += alloc->mHal.drvState.lod[lod].stride; 445 } 446 // TODO: handle YUV Allocations 447 if (alloc->mHal.state.yuv) { 448 size_t clineSize = lineSize; 449 int lod = 1; 450 int maxLod = 2; 451 if (alloc->mHal.state.yuv == HAL_PIXEL_FORMAT_YV12) { 452 maxLod = 3; 453 clineSize >>= 1; 454 } else if (alloc->mHal.state.yuv == HAL_PIXEL_FORMAT_YCrCb_420_SP) { 455 lod = 2; 456 maxLod = 3; 457 } 458 459 while (lod < maxLod) { 460 uint8_t *dst = GetOffsetPtr(alloc, xoff, yoff, 0, lod, face); 461 462 for (uint32_t line = (yoff >> 1); line < ((yoff + h) >> 1); line++) { 463 memcpy(dst, src, clineSize); 464 // When copying from an array to an Allocation, the src pointer 465 // to the array should just move by the number of bytes copied. 466 src += clineSize; 467 dst += alloc->mHal.drvState.lod[lod].stride; 468 } 469 lod++; 470 } 471 } 472 } 473} 474 475void rsovAllocationData3D(const Context *rsc, const Allocation *alloc, 476 uint32_t xoff, uint32_t yoff, uint32_t zoff, 477 uint32_t lod, uint32_t w, uint32_t h, uint32_t d, 478 const void *data, size_t sizeBytes, size_t stride) { 479 uint32_t eSize = alloc->mHal.state.elementSizeBytes; 480 uint32_t lineSize = eSize * w; 481 if (!stride) { 482 stride = lineSize; 483 } 484 485 if (alloc->mHal.drvState.lod[0].mallocPtr) { 486 const uint8_t *src = static_cast<const uint8_t *>(data); 487 for (uint32_t z = zoff; z < (d + zoff); z++) { 488 uint8_t *dst = GetOffsetPtr(alloc, xoff, yoff, z, lod, 489 RS_ALLOCATION_CUBEMAP_FACE_POSITIVE_X); 490 for (uint32_t line = yoff; line < (yoff + h); line++) { 491 if (alloc->mHal.state.hasReferences) { 492 alloc->incRefs(src, w); 493 alloc->decRefs(dst, w); 494 } 495 memcpy(dst, src, lineSize); 496 src += stride; 497 dst += alloc->mHal.drvState.lod[lod].stride; 498 } 499 } 500 } 501} 502 503void rsovAllocationRead1D(const Context *rsc, const Allocation *alloc, 504 uint32_t xoff, uint32_t lod, size_t count, void *data, 505 size_t sizeBytes) { 506 const size_t eSize = alloc->mHal.state.type->getElementSizeBytes(); 507 const uint8_t *ptr = 508 GetOffsetPtr(alloc, xoff, 0, 0, 0, RS_ALLOCATION_CUBEMAP_FACE_POSITIVE_X); 509 if (data != ptr) { 510 // Skip the copy if we are the same allocation. This can arise from 511 // our Bitmap optimization, where we share the same storage. 512 memcpy(data, ptr, count * eSize); 513 } 514} 515 516void rsovAllocationRead2D(const Context *rsc, const Allocation *alloc, 517 uint32_t xoff, uint32_t yoff, uint32_t lod, 518 RsAllocationCubemapFace face, uint32_t w, uint32_t h, 519 void *data, size_t sizeBytes, size_t stride) { 520 size_t eSize = alloc->mHal.state.elementSizeBytes; 521 size_t lineSize = eSize * w; 522 if (!stride) { 523 stride = lineSize; 524 } 525 526 if (alloc->mHal.drvState.lod[0].mallocPtr) { 527 uint8_t *dst = static_cast<uint8_t *>(data); 528 const uint8_t *src = GetOffsetPtr(alloc, xoff, yoff, 0, lod, face); 529 if (dst == src) { 530 // Skip the copy if we are the same allocation. This can arise from 531 // our Bitmap optimization, where we share the same storage. 532 return; 533 } 534 535 for (uint32_t line = yoff; line < (yoff + h); line++) { 536 memcpy(dst, src, lineSize); 537 dst += stride; 538 src += alloc->mHal.drvState.lod[lod].stride; 539 } 540 } else { 541 ALOGE("Add code to readback from non-script memory"); 542 } 543} 544 545void rsovAllocationRead3D(const Context *rsc, const Allocation *alloc, 546 uint32_t xoff, uint32_t yoff, uint32_t zoff, 547 uint32_t lod, uint32_t w, uint32_t h, uint32_t d, 548 void *data, size_t sizeBytes, size_t stride) { 549 uint32_t eSize = alloc->mHal.state.elementSizeBytes; 550 uint32_t lineSize = eSize * w; 551 if (!stride) { 552 stride = lineSize; 553 } 554 555 if (alloc->mHal.drvState.lod[0].mallocPtr) { 556 uint8_t *dst = static_cast<uint8_t *>(data); 557 for (uint32_t z = zoff; z < (d + zoff); z++) { 558 const uint8_t *src = GetOffsetPtr(alloc, xoff, yoff, z, lod, 559 RS_ALLOCATION_CUBEMAP_FACE_POSITIVE_X); 560 if (dst == src) { 561 // Skip the copy if we are the same allocation. This can arise from 562 // our Bitmap optimization, where we share the same storage. 563 return; 564 } 565 566 for (uint32_t line = yoff; line < (yoff + h); line++) { 567 memcpy(dst, src, lineSize); 568 dst += stride; 569 src += alloc->mHal.drvState.lod[lod].stride; 570 } 571 } 572 } 573} 574 575void *rsovAllocationLock1D(const Context *rsc, const Allocation *alloc) { 576 return alloc->mHal.drvState.lod[0].mallocPtr; 577} 578 579void rsovAllocationUnlock1D(const Context *rsc, const Allocation *alloc) {} 580 581void rsovAllocationData1D_alloc(const Context *rsc, const Allocation *dstAlloc, 582 uint32_t dstXoff, uint32_t dstLod, size_t count, 583 const Allocation *srcAlloc, uint32_t srcXoff, 584 uint32_t srcLod) {} 585 586void rsovAllocationData2D_alloc_script( 587 const Context *rsc, const Allocation *dstAlloc, uint32_t dstXoff, 588 uint32_t dstYoff, uint32_t dstLod, RsAllocationCubemapFace dstFace, 589 uint32_t w, uint32_t h, const Allocation *srcAlloc, uint32_t srcXoff, 590 uint32_t srcYoff, uint32_t srcLod, RsAllocationCubemapFace srcFace) { 591 size_t elementSize = dstAlloc->getType()->getElementSizeBytes(); 592 for (uint32_t i = 0; i < h; i++) { 593 uint8_t *dstPtr = 594 GetOffsetPtr(dstAlloc, dstXoff, dstYoff + i, 0, dstLod, dstFace); 595 uint8_t *srcPtr = 596 GetOffsetPtr(srcAlloc, srcXoff, srcYoff + i, 0, srcLod, srcFace); 597 memcpy(dstPtr, srcPtr, w * elementSize); 598 } 599} 600 601void rsovAllocationData3D_alloc_script( 602 const Context *rsc, const Allocation *dstAlloc, uint32_t dstXoff, 603 uint32_t dstYoff, uint32_t dstZoff, uint32_t dstLod, uint32_t w, uint32_t h, 604 uint32_t d, const Allocation *srcAlloc, uint32_t srcXoff, uint32_t srcYoff, 605 uint32_t srcZoff, uint32_t srcLod) { 606 uint32_t elementSize = dstAlloc->getType()->getElementSizeBytes(); 607 for (uint32_t j = 0; j < d; j++) { 608 for (uint32_t i = 0; i < h; i++) { 609 uint8_t *dstPtr = 610 GetOffsetPtr(dstAlloc, dstXoff, dstYoff + i, dstZoff + j, dstLod, 611 RS_ALLOCATION_CUBEMAP_FACE_POSITIVE_X); 612 uint8_t *srcPtr = 613 GetOffsetPtr(srcAlloc, srcXoff, srcYoff + i, srcZoff + j, srcLod, 614 RS_ALLOCATION_CUBEMAP_FACE_POSITIVE_X); 615 memcpy(dstPtr, srcPtr, w * elementSize); 616 } 617 } 618} 619 620void rsovAllocationData2D_alloc( 621 const Context *rsc, const Allocation *dstAlloc, uint32_t dstXoff, 622 uint32_t dstYoff, uint32_t dstLod, RsAllocationCubemapFace dstFace, 623 uint32_t w, uint32_t h, const Allocation *srcAlloc, uint32_t srcXoff, 624 uint32_t srcYoff, uint32_t srcLod, RsAllocationCubemapFace srcFace) { 625 if (!dstAlloc->getIsScript() && !srcAlloc->getIsScript()) { 626 rsc->setError(RS_ERROR_FATAL_DRIVER, 627 "Non-script allocation copies not " 628 "yet implemented."); 629 return; 630 } 631 rsovAllocationData2D_alloc_script(rsc, dstAlloc, dstXoff, dstYoff, dstLod, 632 dstFace, w, h, srcAlloc, srcXoff, srcYoff, 633 srcLod, srcFace); 634} 635 636void rsovAllocationData3D_alloc(const Context *rsc, const Allocation *dstAlloc, 637 uint32_t dstXoff, uint32_t dstYoff, 638 uint32_t dstZoff, uint32_t dstLod, uint32_t w, 639 uint32_t h, uint32_t d, 640 const Allocation *srcAlloc, uint32_t srcXoff, 641 uint32_t srcYoff, uint32_t srcZoff, 642 uint32_t srcLod) { 643 if (!dstAlloc->getIsScript() && !srcAlloc->getIsScript()) { 644 rsc->setError(RS_ERROR_FATAL_DRIVER, 645 "Non-script allocation copies not " 646 "yet implemented."); 647 return; 648 } 649 rsovAllocationData3D_alloc_script(rsc, dstAlloc, dstXoff, dstYoff, dstZoff, 650 dstLod, w, h, d, srcAlloc, srcXoff, srcYoff, 651 srcZoff, srcLod); 652} 653 654void rsovAllocationAdapterOffset(const Context *rsc, const Allocation *alloc) { 655 // Get a base pointer to the new LOD 656 const Allocation *base = alloc->mHal.state.baseAlloc; 657 const Type *type = alloc->mHal.state.type; 658 if (base == nullptr) { 659 return; 660 } 661 662 const int lodBias = alloc->mHal.state.originLOD; 663 uint32_t lodCount = rsMax(alloc->mHal.drvState.lodCount, (uint32_t)1); 664 for (uint32_t lod = 0; lod < lodCount; lod++) { 665 alloc->mHal.drvState.lod[lod] = base->mHal.drvState.lod[lod + lodBias]; 666 alloc->mHal.drvState.lod[lod].mallocPtr = GetOffsetPtr( 667 alloc, alloc->mHal.state.originX, alloc->mHal.state.originY, 668 alloc->mHal.state.originZ, lodBias, 669 (RsAllocationCubemapFace)alloc->mHal.state.originFace); 670 } 671} 672 673bool rsovAllocationAdapterInit(const Context *rsc, Allocation *alloc) { 674// TODO: may need a RSoV Allocation here 675#if 0 676 DrvAllocation *drv = (DrvAllocation *)calloc(1, sizeof(DrvAllocation)); 677 if (!drv) { 678 return false; 679 } 680 alloc->mHal.drv = drv; 681#endif 682 // We need to build an allocation that looks like a subset of the parent 683 // allocation 684 rsovAllocationAdapterOffset(rsc, alloc); 685 686 return true; 687} 688 689void rsovAllocationSyncAll(const Context *rsc, const Allocation *alloc, 690 RsAllocationUsageType src) { 691 // TODO: anything to do here? 692} 693 694void rsovAllocationMarkDirty(const Context *rsc, const Allocation *alloc) { 695 // TODO: anything to do here? 696} 697 698void rsovAllocationResize(const Context *rsc, const Allocation *alloc, 699 const Type *newType, bool zeroNew) { 700 // TODO: implement this 701 // can this be done without copying, if the new size is greater than the 702 // original? 703} 704 705void rsovAllocationGenerateMipmaps(const Context *rsc, 706 const Allocation *alloc) { 707 if (!alloc->mHal.drvState.lod[0].mallocPtr) { 708 return; 709 } 710 uint32_t numFaces = alloc->getType()->getDimFaces() ? 6 : 1; 711 for (uint32_t face = 0; face < numFaces; face++) { 712 for (uint32_t lod = 0; lod < (alloc->getType()->getLODCount() - 1); lod++) { 713 switch (alloc->getType()->getElement()->getSizeBits()) { 714 case 32: 715 mip8888(alloc, lod, (RsAllocationCubemapFace)face); 716 break; 717 case 16: 718 mip565(alloc, lod, (RsAllocationCubemapFace)face); 719 break; 720 case 8: 721 mip8(alloc, lod, (RsAllocationCubemapFace)face); 722 break; 723 } 724 } 725 } 726} 727 728uint32_t rsovAllocationGrallocBits(const Context *rsc, Allocation *alloc) { 729 return 0; 730} 731 732void rsovAllocationUpdateCachedObject(const Context *rsc, 733 const Allocation *alloc, 734 rs_allocation *obj) { 735 obj->p = alloc; 736#ifdef __LP64__ 737 if (alloc != nullptr) { 738 obj->r = alloc->mHal.drvState.lod[0].mallocPtr; 739 obj->v1 = alloc->mHal.drv; 740 obj->v2 = (void *)alloc->mHal.drvState.lod[0].stride; 741 } else { 742 obj->r = nullptr; 743 obj->v1 = nullptr; 744 obj->v2 = nullptr; 745 } 746#endif 747} 748 749void rsovAllocationSetSurface(const Context *rsc, Allocation *alloc, 750 ANativeWindow *nw) { 751 // TODO: implement this 752} 753 754void rsovAllocationIoSend(const Context *rsc, Allocation *alloc) { 755 // TODO: implement this 756} 757 758void rsovAllocationIoReceive(const Context *rsc, Allocation *alloc) { 759 // TODO: implement this 760} 761 762void rsovAllocationElementData(const Context *rsc, const Allocation *alloc, 763 uint32_t x, uint32_t y, uint32_t z, 764 const void *data, uint32_t cIdx, 765 size_t sizeBytes) { 766 uint8_t *ptr = 767 GetOffsetPtr(alloc, x, y, z, 0, RS_ALLOCATION_CUBEMAP_FACE_POSITIVE_X); 768 769 const Element *e = alloc->mHal.state.type->getElement()->getField(cIdx); 770 ptr += alloc->mHal.state.type->getElement()->getFieldOffsetBytes(cIdx); 771 772 if (alloc->mHal.state.hasReferences) { 773 e->incRefs(data); 774 e->decRefs(ptr); 775 } 776 777 memcpy(ptr, data, sizeBytes); 778} 779 780void rsovAllocationElementRead(const Context *rsc, const Allocation *alloc, 781 uint32_t x, uint32_t y, uint32_t z, void *data, 782 uint32_t cIdx, size_t sizeBytes) { 783 uint8_t *ptr = 784 GetOffsetPtr(alloc, x, y, z, 0, RS_ALLOCATION_CUBEMAP_FACE_POSITIVE_X); 785 786 const Element *e = alloc->mHal.state.type->getElement()->getField(cIdx); 787 ptr += alloc->mHal.state.type->getElement()->getFieldOffsetBytes(cIdx); 788 789 memcpy(data, ptr, sizeBytes); 790} 791