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