rsdAllocation.cpp revision a572aca4eb4ddb32c10baa1f529431cfefd756b8
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 alloc->mHal.drvState.lod[0].stride = alloc->mHal.drvState.lod[0].dimX * type->getElementSizeBytes(); 249 alloc->mHal.drvState.lodCount = type->getLODCount(); 250 alloc->mHal.drvState.faceCount = type->getDimFaces(); 251 252 size_t offsets[Allocation::MAX_LOD]; 253 memset(offsets, 0, sizeof(offsets)); 254 255 size_t o = alloc->mHal.drvState.lod[0].stride * rsMax(alloc->mHal.drvState.lod[0].dimY, 1u) * 256 rsMax(alloc->mHal.drvState.lod[0].dimZ, 1u); 257 if(alloc->mHal.drvState.lodCount > 1) { 258 uint32_t tx = alloc->mHal.drvState.lod[0].dimX; 259 uint32_t ty = alloc->mHal.drvState.lod[0].dimY; 260 uint32_t tz = alloc->mHal.drvState.lod[0].dimZ; 261 for (uint32_t lod=1; lod < alloc->mHal.drvState.lodCount; lod++) { 262 alloc->mHal.drvState.lod[lod].dimX = tx; 263 alloc->mHal.drvState.lod[lod].dimY = ty; 264 alloc->mHal.drvState.lod[lod].dimZ = tz; 265 alloc->mHal.drvState.lod[lod].stride = tx * type->getElementSizeBytes(); 266 offsets[lod] = o; 267 o += alloc->mHal.drvState.lod[lod].stride * rsMax(ty, 1u) * rsMax(tz, 1u); 268 if (tx > 1) tx >>= 1; 269 if (ty > 1) ty >>= 1; 270 if (tz > 1) tz >>= 1; 271 } 272 } 273 alloc->mHal.drvState.faceOffset = o; 274 275 alloc->mHal.drvState.lod[0].mallocPtr = ptr; 276 for (uint32_t lod=1; lod < alloc->mHal.drvState.lodCount; lod++) { 277 alloc->mHal.drvState.lod[lod].mallocPtr = ptr + offsets[lod]; 278 } 279 280 size_t allocSize = alloc->mHal.drvState.faceOffset; 281 if(alloc->mHal.drvState.faceCount) { 282 allocSize *= 6; 283 } 284 285 return allocSize; 286} 287 288bool rsdAllocationInit(const Context *rsc, Allocation *alloc, bool forceZero) { 289 DrvAllocation *drv = (DrvAllocation *)calloc(1, sizeof(DrvAllocation)); 290 if (!drv) { 291 return false; 292 } 293 alloc->mHal.drv = drv; 294 295 // Calculate the object size. 296 size_t allocSize = AllocationBuildPointerTable(rsc, alloc, alloc->getType(), NULL); 297 298 uint8_t * ptr = NULL; 299 if (alloc->mHal.state.usageFlags & RS_ALLOCATION_USAGE_IO_OUTPUT) { 300 } else if (alloc->mHal.state.userProvidedPtr != NULL) { 301 // user-provided allocation 302 // limitations: no faces, no LOD, USAGE_SCRIPT only 303 if (alloc->mHal.state.usageFlags != (RS_ALLOCATION_USAGE_SCRIPT | RS_ALLOCATION_USAGE_SHARED)) { 304 ALOGE("Can't use user-allocated buffers if usage is not USAGE_SCRIPT and USAGE_SHARED"); 305 return false; 306 } 307 if (alloc->getType()->getDimLOD() || alloc->getType()->getDimFaces()) { 308 ALOGE("User-allocated buffers must not have multiple faces or LODs"); 309 return false; 310 } 311 ptr = (uint8_t*)alloc->mHal.state.userProvidedPtr; 312 } else { 313 if (forceZero) { 314 ptr = (uint8_t *)calloc(1, allocSize); 315 } else { 316 ptr = (uint8_t *)malloc(allocSize); 317 } 318 if (!ptr) { 319 alloc->mHal.drv = NULL; 320 free(drv); 321 return false; 322 } 323 } 324 // Build the pointer tables 325 size_t verifySize = AllocationBuildPointerTable(rsc, alloc, alloc->getType(), ptr); 326 if(allocSize != verifySize) { 327 rsAssert(!"Size mismatch"); 328 } 329 330 drv->glTarget = GL_NONE; 331 if (alloc->mHal.state.usageFlags & RS_ALLOCATION_USAGE_GRAPHICS_TEXTURE) { 332 if (alloc->mHal.state.hasFaces) { 333 drv->glTarget = GL_TEXTURE_CUBE_MAP; 334 } else { 335 drv->glTarget = GL_TEXTURE_2D; 336 } 337 } else { 338 if (alloc->mHal.state.usageFlags & RS_ALLOCATION_USAGE_GRAPHICS_VERTEX) { 339 drv->glTarget = GL_ARRAY_BUFFER; 340 } 341 } 342 343#ifndef RS_COMPATIBILITY_LIB 344 drv->glType = rsdTypeToGLType(alloc->mHal.state.type->getElement()->getComponent().getType()); 345 drv->glFormat = rsdKindToGLFormat(alloc->mHal.state.type->getElement()->getComponent().getKind()); 346#else 347 drv->glType = 0; 348 drv->glFormat = 0; 349#endif 350 351 if (alloc->mHal.state.usageFlags & ~RS_ALLOCATION_USAGE_SCRIPT) { 352 drv->uploadDeferred = true; 353 } 354 355 356 drv->readBackFBO = NULL; 357 358 return true; 359} 360 361void rsdAllocationDestroy(const Context *rsc, Allocation *alloc) { 362 DrvAllocation *drv = (DrvAllocation *)alloc->mHal.drv; 363 364#ifndef RS_COMPATIBILITY_LIB 365 if (drv->bufferID) { 366 // Causes a SW crash.... 367 //ALOGV(" mBufferID %i", mBufferID); 368 //glDeleteBuffers(1, &mBufferID); 369 //mBufferID = 0; 370 } 371 if (drv->textureID) { 372 RSD_CALL_GL(glDeleteTextures, 1, &drv->textureID); 373 drv->textureID = 0; 374 } 375 if (drv->renderTargetID) { 376 RSD_CALL_GL(glDeleteRenderbuffers, 1, &drv->renderTargetID); 377 drv->renderTargetID = 0; 378 } 379#endif 380 381 if (alloc->mHal.drvState.lod[0].mallocPtr) { 382 // don't free user-allocated ptrs 383 if (!(alloc->mHal.state.usageFlags & RS_ALLOCATION_USAGE_SHARED)) { 384 free(alloc->mHal.drvState.lod[0].mallocPtr); 385 } 386 alloc->mHal.drvState.lod[0].mallocPtr = NULL; 387 } 388 389#ifndef RS_COMPATIBILITY_LIB 390 if (drv->readBackFBO != NULL) { 391 delete drv->readBackFBO; 392 drv->readBackFBO = NULL; 393 } 394#endif 395 396 free(drv); 397 alloc->mHal.drv = NULL; 398} 399 400void rsdAllocationResize(const Context *rsc, const Allocation *alloc, 401 const Type *newType, bool zeroNew) { 402 const uint32_t oldDimX = alloc->mHal.drvState.lod[0].dimX; 403 const uint32_t dimX = newType->getDimX(); 404 405 // can't resize Allocations with user-allocated buffers 406 if (alloc->mHal.state.usageFlags & RS_ALLOCATION_USAGE_SHARED) { 407 ALOGE("Resize cannot be called on a USAGE_SHARED allocation"); 408 return; 409 } 410 void * oldPtr = alloc->mHal.drvState.lod[0].mallocPtr; 411 // Calculate the object size 412 size_t s = AllocationBuildPointerTable(rsc, alloc, newType, NULL); 413 uint8_t *ptr = (uint8_t *)realloc(oldPtr, s); 414 // Build the relative pointer tables. 415 size_t verifySize = AllocationBuildPointerTable(rsc, alloc, newType, ptr); 416 if(s != verifySize) { 417 rsAssert(!"Size mismatch"); 418 } 419 420 421 if (dimX > oldDimX) { 422 uint32_t stride = alloc->mHal.state.elementSizeBytes; 423 memset(((uint8_t *)alloc->mHal.drvState.lod[0].mallocPtr) + stride * oldDimX, 424 0, stride * (dimX - oldDimX)); 425 } 426} 427 428static void rsdAllocationSyncFromFBO(const Context *rsc, const Allocation *alloc) { 429#ifndef RS_COMPATIBILITY_LIB 430 if (!alloc->getIsScript()) { 431 return; // nothing to sync 432 } 433 434 RsdHal *dc = (RsdHal *)rsc->mHal.drv; 435 RsdFrameBufferObj *lastFbo = dc->gl.currentFrameBuffer; 436 437 DrvAllocation *drv = (DrvAllocation *)alloc->mHal.drv; 438 if (!drv->textureID && !drv->renderTargetID) { 439 return; // nothing was rendered here yet, so nothing to sync 440 } 441 if (drv->readBackFBO == NULL) { 442 drv->readBackFBO = new RsdFrameBufferObj(); 443 drv->readBackFBO->setColorTarget(drv, 0); 444 drv->readBackFBO->setDimensions(alloc->getType()->getDimX(), 445 alloc->getType()->getDimY()); 446 } 447 448 // Bind the framebuffer object so we can read back from it 449 drv->readBackFBO->setActive(rsc); 450 451 // Do the readback 452 RSD_CALL_GL(glReadPixels, 0, 0, alloc->mHal.drvState.lod[0].dimX, 453 alloc->mHal.drvState.lod[0].dimY, 454 drv->glFormat, drv->glType, alloc->mHal.drvState.lod[0].mallocPtr); 455 456 // Revert framebuffer to its original 457 lastFbo->setActive(rsc); 458#endif 459} 460 461 462void rsdAllocationSyncAll(const Context *rsc, const Allocation *alloc, 463 RsAllocationUsageType src) { 464 DrvAllocation *drv = (DrvAllocation *)alloc->mHal.drv; 465 466 if (src == RS_ALLOCATION_USAGE_GRAPHICS_RENDER_TARGET) { 467 if(!alloc->getIsRenderTarget()) { 468 rsc->setError(RS_ERROR_FATAL_DRIVER, 469 "Attempting to sync allocation from render target, " 470 "for non-render target allocation"); 471 } else if (alloc->getType()->getElement()->getKind() != RS_KIND_PIXEL_RGBA) { 472 rsc->setError(RS_ERROR_FATAL_DRIVER, "Cannot only sync from RGBA" 473 "render target"); 474 } else { 475 rsdAllocationSyncFromFBO(rsc, alloc); 476 } 477 return; 478 } 479 480 rsAssert(src == RS_ALLOCATION_USAGE_SCRIPT); 481 482 if (alloc->mHal.state.usageFlags & RS_ALLOCATION_USAGE_GRAPHICS_TEXTURE) { 483 UploadToTexture(rsc, alloc); 484 } else { 485 if ((alloc->mHal.state.usageFlags & RS_ALLOCATION_USAGE_GRAPHICS_RENDER_TARGET) && 486 !(alloc->mHal.state.usageFlags & RS_ALLOCATION_USAGE_IO_OUTPUT)) { 487 AllocateRenderTarget(rsc, alloc); 488 } 489 } 490 if (alloc->mHal.state.usageFlags & RS_ALLOCATION_USAGE_GRAPHICS_VERTEX) { 491 UploadToBufferObject(rsc, alloc); 492 } 493 494 drv->uploadDeferred = false; 495} 496 497void rsdAllocationMarkDirty(const Context *rsc, const Allocation *alloc) { 498 DrvAllocation *drv = (DrvAllocation *)alloc->mHal.drv; 499 drv->uploadDeferred = true; 500} 501 502int32_t rsdAllocationInitSurfaceTexture(const Context *rsc, const Allocation *alloc) { 503#ifndef RS_COMPATIBILITY_LIB 504 DrvAllocation *drv = (DrvAllocation *)alloc->mHal.drv; 505 UploadToTexture(rsc, alloc); 506 return drv->textureID; 507#else 508 return 0; 509#endif 510} 511 512#ifndef RS_COMPATIBILITY_LIB 513static bool IoGetBuffer(const Context *rsc, Allocation *alloc, ANativeWindow *nw) { 514 DrvAllocation *drv = (DrvAllocation *)alloc->mHal.drv; 515 516 int32_t r = native_window_dequeue_buffer_and_wait(nw, &drv->wndBuffer); 517 if (r) { 518 rsc->setError(RS_ERROR_DRIVER, "Error getting next IO output buffer."); 519 return false; 520 } 521 522 // Must lock the whole surface 523 GraphicBufferMapper &mapper = GraphicBufferMapper::get(); 524 Rect bounds(drv->wndBuffer->width, drv->wndBuffer->height); 525 526 void *dst = NULL; 527 mapper.lock(drv->wndBuffer->handle, 528 GRALLOC_USAGE_SW_READ_NEVER | GRALLOC_USAGE_SW_WRITE_OFTEN, 529 bounds, &dst); 530 alloc->mHal.drvState.lod[0].mallocPtr = dst; 531 alloc->mHal.drvState.lod[0].stride = drv->wndBuffer->stride * alloc->mHal.state.elementSizeBytes; 532 533 return true; 534} 535#endif 536 537void rsdAllocationSetSurfaceTexture(const Context *rsc, Allocation *alloc, ANativeWindow *nw) { 538#ifndef RS_COMPATIBILITY_LIB 539 DrvAllocation *drv = (DrvAllocation *)alloc->mHal.drv; 540 541 //ALOGE("rsdAllocationSetSurfaceTexture %p %p", alloc, nw); 542 543 if (alloc->mHal.state.usageFlags & RS_ALLOCATION_USAGE_GRAPHICS_RENDER_TARGET) { 544 //TODO finish support for render target + script 545 drv->wnd = nw; 546 return; 547 } 548 549 550 // Cleanup old surface if there is one. 551 if (alloc->mHal.state.wndSurface) { 552 ANativeWindow *old = alloc->mHal.state.wndSurface; 553 GraphicBufferMapper &mapper = GraphicBufferMapper::get(); 554 mapper.unlock(drv->wndBuffer->handle); 555 old->queueBuffer(old, drv->wndBuffer, -1); 556 } 557 558 if (nw != NULL) { 559 int32_t r; 560 uint32_t flags = 0; 561 if (alloc->mHal.state.usageFlags & RS_ALLOCATION_USAGE_SCRIPT) { 562 flags |= GRALLOC_USAGE_SW_READ_RARELY | GRALLOC_USAGE_SW_WRITE_OFTEN; 563 } 564 if (alloc->mHal.state.usageFlags & RS_ALLOCATION_USAGE_GRAPHICS_RENDER_TARGET) { 565 flags |= GRALLOC_USAGE_HW_RENDER; 566 } 567 568 r = native_window_set_usage(nw, flags); 569 if (r) { 570 rsc->setError(RS_ERROR_DRIVER, "Error setting IO output buffer usage."); 571 return; 572 } 573 574 r = native_window_set_buffers_dimensions(nw, alloc->mHal.drvState.lod[0].dimX, 575 alloc->mHal.drvState.lod[0].dimY); 576 if (r) { 577 rsc->setError(RS_ERROR_DRIVER, "Error setting IO output buffer dimensions."); 578 return; 579 } 580 581 r = native_window_set_buffer_count(nw, 3); 582 if (r) { 583 rsc->setError(RS_ERROR_DRIVER, "Error setting IO output buffer count."); 584 return; 585 } 586 587 IoGetBuffer(rsc, alloc, nw); 588 } 589#endif 590} 591 592void rsdAllocationIoSend(const Context *rsc, Allocation *alloc) { 593#ifndef RS_COMPATIBILITY_LIB 594 DrvAllocation *drv = (DrvAllocation *)alloc->mHal.drv; 595 ANativeWindow *nw = alloc->mHal.state.wndSurface; 596 597 if (alloc->mHal.state.usageFlags & RS_ALLOCATION_USAGE_GRAPHICS_RENDER_TARGET) { 598 RsdHal *dc = (RsdHal *)rsc->mHal.drv; 599 RSD_CALL_GL(eglSwapBuffers, dc->gl.egl.display, dc->gl.egl.surface); 600 return; 601 } 602 603 if (alloc->mHal.state.usageFlags & RS_ALLOCATION_USAGE_SCRIPT) { 604 GraphicBufferMapper &mapper = GraphicBufferMapper::get(); 605 mapper.unlock(drv->wndBuffer->handle); 606 int32_t r = nw->queueBuffer(nw, drv->wndBuffer, -1); 607 if (r) { 608 rsc->setError(RS_ERROR_DRIVER, "Error sending IO output buffer."); 609 return; 610 } 611 612 IoGetBuffer(rsc, alloc, nw); 613 } 614#endif 615} 616 617void rsdAllocationIoReceive(const Context *rsc, Allocation *alloc) { 618#ifndef RS_COMPATIBILITY_LIB 619 DrvAllocation *drv = (DrvAllocation *)alloc->mHal.drv; 620 alloc->mHal.state.surfaceTexture->updateTexImage(); 621#endif 622} 623 624 625void rsdAllocationData1D(const Context *rsc, const Allocation *alloc, 626 uint32_t xoff, uint32_t lod, uint32_t count, 627 const void *data, size_t sizeBytes) { 628 DrvAllocation *drv = (DrvAllocation *)alloc->mHal.drv; 629 630 const uint32_t eSize = alloc->mHal.state.type->getElementSizeBytes(); 631 uint8_t * ptr = GetOffsetPtr(alloc, xoff, 0, 0, RS_ALLOCATION_CUBEMAP_FACE_POSITIVE_X); 632 uint32_t size = count * eSize; 633 634 if (ptr != data) { 635 // Skip the copy if we are the same allocation. This can arise from 636 // our Bitmap optimization, where we share the same storage. 637 if (alloc->mHal.state.hasReferences) { 638 alloc->incRefs(data, count); 639 alloc->decRefs(ptr, count); 640 } 641 memcpy(ptr, data, size); 642 } 643 drv->uploadDeferred = true; 644} 645 646void rsdAllocationData2D(const Context *rsc, const Allocation *alloc, 647 uint32_t xoff, uint32_t yoff, uint32_t lod, RsAllocationCubemapFace face, 648 uint32_t w, uint32_t h, const void *data, size_t sizeBytes, size_t stride) { 649 DrvAllocation *drv = (DrvAllocation *)alloc->mHal.drv; 650 651 uint32_t eSize = alloc->mHal.state.elementSizeBytes; 652 uint32_t lineSize = eSize * w; 653 if (!stride) { 654 stride = lineSize; 655 } 656 657 if (alloc->mHal.drvState.lod[0].mallocPtr) { 658 const uint8_t *src = static_cast<const uint8_t *>(data); 659 uint8_t *dst = GetOffsetPtr(alloc, xoff, yoff, lod, face); 660 if (dst == src) { 661 // Skip the copy if we are the same allocation. This can arise from 662 // our Bitmap optimization, where we share the same storage. 663 drv->uploadDeferred = true; 664 return; 665 } 666 667 for (uint32_t line=yoff; line < (yoff+h); line++) { 668 if (alloc->mHal.state.hasReferences) { 669 alloc->incRefs(src, w); 670 alloc->decRefs(dst, w); 671 } 672 memcpy(dst, src, lineSize); 673 src += stride; 674 dst += alloc->mHal.drvState.lod[lod].stride; 675 } 676 drv->uploadDeferred = true; 677 } else { 678 Update2DTexture(rsc, alloc, data, xoff, yoff, lod, face, w, h); 679 } 680} 681 682void rsdAllocationData3D(const Context *rsc, const Allocation *alloc, 683 uint32_t xoff, uint32_t yoff, uint32_t zoff, 684 uint32_t lod, RsAllocationCubemapFace face, 685 uint32_t w, uint32_t h, uint32_t d, const void *data, uint32_t sizeBytes) { 686 687} 688 689void rsdAllocationRead1D(const Context *rsc, const Allocation *alloc, 690 uint32_t xoff, uint32_t lod, uint32_t count, 691 void *data, size_t sizeBytes) { 692 const uint32_t eSize = alloc->mHal.state.type->getElementSizeBytes(); 693 const uint8_t * ptr = GetOffsetPtr(alloc, xoff, 0, 0, RS_ALLOCATION_CUBEMAP_FACE_POSITIVE_X); 694 if (data != ptr) { 695 // Skip the copy if we are the same allocation. This can arise from 696 // our Bitmap optimization, where we share the same storage. 697 memcpy(data, ptr, count * eSize); 698 } 699} 700 701void rsdAllocationRead2D(const Context *rsc, const Allocation *alloc, 702 uint32_t xoff, uint32_t yoff, uint32_t lod, RsAllocationCubemapFace face, 703 uint32_t w, uint32_t h, void *data, size_t sizeBytes, size_t stride) { 704 uint32_t eSize = alloc->mHal.state.elementSizeBytes; 705 uint32_t lineSize = eSize * w; 706 if (!stride) { 707 stride = lineSize; 708 } 709 710 if (alloc->mHal.drvState.lod[0].mallocPtr) { 711 uint8_t *dst = static_cast<uint8_t *>(data); 712 const uint8_t *src = GetOffsetPtr(alloc, xoff, yoff, lod, face); 713 if (dst == src) { 714 // Skip the copy if we are the same allocation. This can arise from 715 // our Bitmap optimization, where we share the same storage. 716 return; 717 } 718 719 for (uint32_t line=yoff; line < (yoff+h); line++) { 720 memcpy(dst, src, lineSize); 721 dst += stride; 722 src += alloc->mHal.drvState.lod[lod].stride; 723 } 724 } else { 725 ALOGE("Add code to readback from non-script memory"); 726 } 727} 728 729 730void rsdAllocationRead3D(const Context *rsc, const Allocation *alloc, 731 uint32_t xoff, uint32_t yoff, uint32_t zoff, 732 uint32_t lod, RsAllocationCubemapFace face, 733 uint32_t w, uint32_t h, uint32_t d, void *data, uint32_t sizeBytes) { 734 735} 736 737void * rsdAllocationLock1D(const android::renderscript::Context *rsc, 738 const android::renderscript::Allocation *alloc) { 739 return alloc->mHal.drvState.lod[0].mallocPtr; 740} 741 742void rsdAllocationUnlock1D(const android::renderscript::Context *rsc, 743 const android::renderscript::Allocation *alloc) { 744 745} 746 747void rsdAllocationData1D_alloc(const android::renderscript::Context *rsc, 748 const android::renderscript::Allocation *dstAlloc, 749 uint32_t dstXoff, uint32_t dstLod, uint32_t count, 750 const android::renderscript::Allocation *srcAlloc, 751 uint32_t srcXoff, uint32_t srcLod) { 752} 753 754 755void rsdAllocationData2D_alloc_script(const android::renderscript::Context *rsc, 756 const android::renderscript::Allocation *dstAlloc, 757 uint32_t dstXoff, uint32_t dstYoff, uint32_t dstLod, 758 RsAllocationCubemapFace dstFace, uint32_t w, uint32_t h, 759 const android::renderscript::Allocation *srcAlloc, 760 uint32_t srcXoff, uint32_t srcYoff, uint32_t srcLod, 761 RsAllocationCubemapFace srcFace) { 762 uint32_t elementSize = dstAlloc->getType()->getElementSizeBytes(); 763 for (uint32_t i = 0; i < h; i ++) { 764 uint8_t *dstPtr = GetOffsetPtr(dstAlloc, dstXoff, dstYoff + i, dstLod, dstFace); 765 uint8_t *srcPtr = GetOffsetPtr(srcAlloc, srcXoff, srcYoff + i, srcLod, srcFace); 766 memcpy(dstPtr, srcPtr, w * elementSize); 767 768 //ALOGE("COPIED dstXoff(%u), dstYoff(%u), dstLod(%u), dstFace(%u), w(%u), h(%u), srcXoff(%u), srcYoff(%u), srcLod(%u), srcFace(%u)", 769 // dstXoff, dstYoff, dstLod, dstFace, w, h, srcXoff, srcYoff, srcLod, srcFace); 770 } 771} 772 773void rsdAllocationData2D_alloc(const android::renderscript::Context *rsc, 774 const android::renderscript::Allocation *dstAlloc, 775 uint32_t dstXoff, uint32_t dstYoff, uint32_t dstLod, 776 RsAllocationCubemapFace dstFace, uint32_t w, uint32_t h, 777 const android::renderscript::Allocation *srcAlloc, 778 uint32_t srcXoff, uint32_t srcYoff, uint32_t srcLod, 779 RsAllocationCubemapFace srcFace) { 780 if (!dstAlloc->getIsScript() && !srcAlloc->getIsScript()) { 781 rsc->setError(RS_ERROR_FATAL_DRIVER, "Non-script allocation copies not " 782 "yet implemented."); 783 return; 784 } 785 rsdAllocationData2D_alloc_script(rsc, dstAlloc, dstXoff, dstYoff, 786 dstLod, dstFace, w, h, srcAlloc, 787 srcXoff, srcYoff, srcLod, srcFace); 788} 789 790void rsdAllocationData3D_alloc(const android::renderscript::Context *rsc, 791 const android::renderscript::Allocation *dstAlloc, 792 uint32_t dstXoff, uint32_t dstYoff, uint32_t dstZoff, 793 uint32_t dstLod, RsAllocationCubemapFace dstFace, 794 uint32_t w, uint32_t h, uint32_t d, 795 const android::renderscript::Allocation *srcAlloc, 796 uint32_t srcXoff, uint32_t srcYoff, uint32_t srcZoff, 797 uint32_t srcLod, RsAllocationCubemapFace srcFace) { 798} 799 800void rsdAllocationElementData1D(const Context *rsc, const Allocation *alloc, 801 uint32_t x, 802 const void *data, uint32_t cIdx, uint32_t sizeBytes) { 803 DrvAllocation *drv = (DrvAllocation *)alloc->mHal.drv; 804 805 uint32_t eSize = alloc->mHal.state.elementSizeBytes; 806 uint8_t * ptr = GetOffsetPtr(alloc, x, 0, 0, RS_ALLOCATION_CUBEMAP_FACE_POSITIVE_X); 807 808 const Element * e = alloc->mHal.state.type->getElement()->getField(cIdx); 809 ptr += alloc->mHal.state.type->getElement()->getFieldOffsetBytes(cIdx); 810 811 if (alloc->mHal.state.hasReferences) { 812 e->incRefs(data); 813 e->decRefs(ptr); 814 } 815 816 memcpy(ptr, data, sizeBytes); 817 drv->uploadDeferred = true; 818} 819 820void rsdAllocationElementData2D(const Context *rsc, const Allocation *alloc, 821 uint32_t x, uint32_t y, 822 const void *data, uint32_t cIdx, uint32_t sizeBytes) { 823 DrvAllocation *drv = (DrvAllocation *)alloc->mHal.drv; 824 825 uint32_t eSize = alloc->mHal.state.elementSizeBytes; 826 uint8_t * ptr = GetOffsetPtr(alloc, x, y, 0, RS_ALLOCATION_CUBEMAP_FACE_POSITIVE_X); 827 828 const Element * e = alloc->mHal.state.type->getElement()->getField(cIdx); 829 ptr += alloc->mHal.state.type->getElement()->getFieldOffsetBytes(cIdx); 830 831 if (alloc->mHal.state.hasReferences) { 832 e->incRefs(data); 833 e->decRefs(ptr); 834 } 835 836 memcpy(ptr, data, sizeBytes); 837 drv->uploadDeferred = true; 838} 839 840static void mip565(const Allocation *alloc, int lod, RsAllocationCubemapFace face) { 841 uint32_t w = alloc->mHal.drvState.lod[lod + 1].dimX; 842 uint32_t h = alloc->mHal.drvState.lod[lod + 1].dimY; 843 844 for (uint32_t y=0; y < h; y++) { 845 uint16_t *oPtr = (uint16_t *)GetOffsetPtr(alloc, 0, y, lod + 1, face); 846 const uint16_t *i1 = (uint16_t *)GetOffsetPtr(alloc, 0, y*2, lod, face); 847 const uint16_t *i2 = (uint16_t *)GetOffsetPtr(alloc, 0, y*2+1, lod, face); 848 849 for (uint32_t x=0; x < w; x++) { 850 *oPtr = rsBoxFilter565(i1[0], i1[1], i2[0], i2[1]); 851 oPtr ++; 852 i1 += 2; 853 i2 += 2; 854 } 855 } 856} 857 858static void mip8888(const Allocation *alloc, int lod, RsAllocationCubemapFace face) { 859 uint32_t w = alloc->mHal.drvState.lod[lod + 1].dimX; 860 uint32_t h = alloc->mHal.drvState.lod[lod + 1].dimY; 861 862 for (uint32_t y=0; y < h; y++) { 863 uint32_t *oPtr = (uint32_t *)GetOffsetPtr(alloc, 0, y, lod + 1, face); 864 const uint32_t *i1 = (uint32_t *)GetOffsetPtr(alloc, 0, y*2, lod, face); 865 const uint32_t *i2 = (uint32_t *)GetOffsetPtr(alloc, 0, y*2+1, lod, face); 866 867 for (uint32_t x=0; x < w; x++) { 868 *oPtr = rsBoxFilter8888(i1[0], i1[1], i2[0], i2[1]); 869 oPtr ++; 870 i1 += 2; 871 i2 += 2; 872 } 873 } 874} 875 876static void mip8(const Allocation *alloc, int lod, RsAllocationCubemapFace face) { 877 uint32_t w = alloc->mHal.drvState.lod[lod + 1].dimX; 878 uint32_t h = alloc->mHal.drvState.lod[lod + 1].dimY; 879 880 for (uint32_t y=0; y < h; y++) { 881 uint8_t *oPtr = GetOffsetPtr(alloc, 0, y, lod + 1, face); 882 const uint8_t *i1 = GetOffsetPtr(alloc, 0, y*2, lod, face); 883 const uint8_t *i2 = GetOffsetPtr(alloc, 0, y*2+1, lod, face); 884 885 for (uint32_t x=0; x < w; x++) { 886 *oPtr = (uint8_t)(((uint32_t)i1[0] + i1[1] + i2[0] + i2[1]) * 0.25f); 887 oPtr ++; 888 i1 += 2; 889 i2 += 2; 890 } 891 } 892} 893 894void rsdAllocationGenerateMipmaps(const Context *rsc, const Allocation *alloc) { 895 if(!alloc->mHal.drvState.lod[0].mallocPtr) { 896 return; 897 } 898 uint32_t numFaces = alloc->getType()->getDimFaces() ? 6 : 1; 899 for (uint32_t face = 0; face < numFaces; face ++) { 900 for (uint32_t lod=0; lod < (alloc->getType()->getLODCount() -1); lod++) { 901 switch (alloc->getType()->getElement()->getSizeBits()) { 902 case 32: 903 mip8888(alloc, lod, (RsAllocationCubemapFace)face); 904 break; 905 case 16: 906 mip565(alloc, lod, (RsAllocationCubemapFace)face); 907 break; 908 case 8: 909 mip8(alloc, lod, (RsAllocationCubemapFace)face); 910 break; 911 } 912 } 913 } 914} 915 916 917