get buffer as Image */
| 526 | |
| 527 | /* get buffer as Image */ |
| 528 | void RenderTexture::newImage(std::function<void(RefPtr<Image>)> imageCallback, bool eglCacheHint) |
| 529 | { |
| 530 | AXASSERT(_pixelFormat == backend::PixelFormat::RGBA8, "only RGBA8888 can be saved as image"); |
| 531 | |
| 532 | if ((nullptr == _texture2D)) |
| 533 | { |
| 534 | return; |
| 535 | } |
| 536 | |
| 537 | const Vec2& s = _texture2D->getContentSizeInPixels(); |
| 538 | |
| 539 | // to get the image size to save |
| 540 | // if the saving image domain exceeds the buffer texture domain, |
| 541 | // it should be cut |
| 542 | int savedBufferWidth = (int)s.width; |
| 543 | int savedBufferHeight = (int)s.height; |
| 544 | bool hasPremultipliedAlpha = _texture2D->hasPremultipliedAlpha(); |
| 545 | |
| 546 | auto callback = [hasPremultipliedAlpha, imageCallback](const backend::PixelBufferDescriptor& pbd) { |
| 547 | if (pbd) |
| 548 | { |
| 549 | auto image = utils::makeInstance<Image>(&Image::initWithRawData, pbd._data.getBytes(), pbd._data.getSize(), |
| 550 | pbd._width, pbd._height, 8, hasPremultipliedAlpha); |
| 551 | imageCallback(image); |
| 552 | } |
| 553 | else |
| 554 | imageCallback(nullptr); |
| 555 | }; |
| 556 | #if defined(AX_USE_GL) |
| 557 | if (eglCacheHint) |
| 558 | { |
| 559 | auto colorAttachment = _renderTarget->_color[0].texture; |
| 560 | if (colorAttachment) |
| 561 | { |
| 562 | backend::PixelBufferDescriptor pbd; |
| 563 | static_cast<backend::CommandBufferGL*>(_director->getRenderer()->getCommandBuffer()) |
| 564 | ->readPixels(_renderTarget, 0, 0, colorAttachment->getWidth(), colorAttachment->getHeight(), |
| 565 | colorAttachment->getWidth() * 4, true, pbd); |
| 566 | callback(pbd); |
| 567 | } |
| 568 | } |
| 569 | else |
| 570 | _director->getRenderer()->readPixels(_renderTarget, callback); |
| 571 | #else |
| 572 | _director->getRenderer()->readPixels(_renderTarget, callback); |
| 573 | #endif |
| 574 | } |
| 575 | |
| 576 | void RenderTexture::draw(Renderer* renderer, const Mat4& transform, uint32_t flags) |
| 577 | { |
no test coverage detected