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