LayerTextureSubImage.cpp [plain text]
#include "config.h"
#if USE(ACCELERATED_COMPOSITING)
#include "LayerTextureSubImage.h"
#include "Extensions3DChromium.h"
#include "GraphicsContext3D.h"
#include "TraceEvent.h"
namespace WebCore {
LayerTextureSubImage::LayerTextureSubImage(bool useMapTexSubImage)
: m_useMapTexSubImage(useMapTexSubImage)
{
}
LayerTextureSubImage::~LayerTextureSubImage()
{
}
void LayerTextureSubImage::setSubImageSize(const IntSize& subImageSize)
{
if (subImageSize == m_subImageSize)
return;
m_subImageSize = subImageSize;
m_subImage.clear();
}
void LayerTextureSubImage::upload(const uint8_t* image, const IntRect& imageRect,
const IntRect& sourceRect, const IntRect& destRect,
GC3Denum format, GraphicsContext3D* context)
{
if (m_useMapTexSubImage)
uploadWithMapTexSubImage(image, imageRect, sourceRect, destRect, format, context);
else
uploadWithTexSubImage(image, imageRect, sourceRect, destRect, format, context);
}
void LayerTextureSubImage::uploadWithTexSubImage(const uint8_t* image, const IntRect& imageRect,
const IntRect& sourceRect, const IntRect& destRect,
GC3Denum format, GraphicsContext3D* context)
{
TRACE_EVENT("LayerTextureSubImage::uploadWithTexSubImage", this, 0);
if (!m_subImage)
m_subImage = adoptArrayPtr(new uint8_t[m_subImageSize.width() * m_subImageSize.height() * 4]);
IntPoint offset(sourceRect.x() - imageRect.x(), sourceRect.y() - imageRect.y());
const uint8_t* pixelSource;
if (imageRect.width() == sourceRect.width() && !offset.x())
pixelSource = &image[4 * offset.y() * imageRect.width()];
else {
for (int row = 0; row < destRect.height(); ++row)
memcpy(&m_subImage[destRect.width() * 4 * row],
&image[4 * (offset.x() + (offset.y() + row) * imageRect.width())],
destRect.width() * 4);
pixelSource = &m_subImage[0];
}
context->texSubImage2D(GraphicsContext3D::TEXTURE_2D, 0, destRect.x(), destRect.y(), destRect.width(), destRect.height(), format, GraphicsContext3D::UNSIGNED_BYTE, pixelSource);
}
void LayerTextureSubImage::uploadWithMapTexSubImage(const uint8_t* image, const IntRect& imageRect,
const IntRect& sourceRect, const IntRect& destRect,
GC3Denum format, GraphicsContext3D* context)
{
TRACE_EVENT("LayerTextureSubImage::uploadWithMapTexSubImage", this, 0);
IntPoint offset(sourceRect.x() - imageRect.x(), sourceRect.y() - imageRect.y());
Extensions3DChromium* extensions = static_cast<Extensions3DChromium*>(context->getExtensions());
uint8_t* pixelDest = static_cast<uint8_t*>(extensions->mapTexSubImage2DCHROMIUM(GraphicsContext3D::TEXTURE_2D, 0, destRect.x(), destRect.y(), destRect.width(), destRect.height(), format, GraphicsContext3D::UNSIGNED_BYTE, Extensions3DChromium::WRITE_ONLY));
if (!pixelDest) {
uploadWithTexSubImage(image, imageRect, sourceRect, destRect, format, context);
return;
}
if (imageRect.width() == sourceRect.width() && !offset.x())
memcpy(pixelDest, &image[4 * offset.y() * imageRect.width()], imageRect.width() * destRect.height() * 4);
else {
for (int row = 0; row < destRect.height(); ++row)
memcpy(&pixelDest[destRect.width() * 4 * row],
&image[4 * (offset.x() + (offset.y() + row) * imageRect.width())],
destRect.width() * 4);
}
extensions->unmapTexSubImage2DCHROMIUM(pixelDest);
}
}
#endif // USE(ACCELERATED_COMPOSITING)