#include "config.h"
#if USE(ACCELERATED_COMPOSITING)
#include "FrameBufferSkPictureCanvasLayerTextureUpdater.h"
#include "CCProxy.h"
#include "LayerPainterChromium.h"
#include "SkCanvas.h"
#include "SkGpuDevice.h"
#include <public/WebGraphicsContext3D.h>
#include <public/WebSharedGraphicsContext3D.h>
using WebKit::WebGraphicsContext3D;
using WebKit::WebSharedGraphicsContext3D;
namespace cc {
static PassOwnPtr<SkCanvas> createAcceleratedCanvas(GrContext* grContext,
IntSize canvasSize,
unsigned textureId)
{
GrPlatformTextureDesc textureDesc;
textureDesc.fFlags = kRenderTarget_GrPlatformTextureFlag;
textureDesc.fWidth = canvasSize.width();
textureDesc.fHeight = canvasSize.height();
textureDesc.fConfig = kSkia8888_GrPixelConfig;
textureDesc.fTextureHandle = textureId;
SkAutoTUnref<GrTexture> target(grContext->createPlatformTexture(textureDesc));
SkAutoTUnref<SkDevice> device(new SkGpuDevice(grContext, target.get()));
return adoptPtr(new SkCanvas(device.get()));
}
FrameBufferSkPictureCanvasLayerTextureUpdater::Texture::Texture(FrameBufferSkPictureCanvasLayerTextureUpdater* textureUpdater, PassOwnPtr<CCPrioritizedTexture> texture)
: LayerTextureUpdater::Texture(texture)
, m_textureUpdater(textureUpdater)
{
}
FrameBufferSkPictureCanvasLayerTextureUpdater::Texture::~Texture()
{
}
void FrameBufferSkPictureCanvasLayerTextureUpdater::Texture::updateRect(CCResourceProvider* resourceProvider, const IntRect& sourceRect, const IntSize& destOffset)
{
WebGraphicsContext3D* sharedContext = CCProxy::hasImplThread() ? WebSharedGraphicsContext3D::compositorThreadContext() : WebSharedGraphicsContext3D::mainThreadContext();
GrContext* sharedGrContext = CCProxy::hasImplThread() ? WebSharedGraphicsContext3D::compositorThreadGrContext() : WebSharedGraphicsContext3D::mainThreadGrContext();
if (!sharedContext || !sharedGrContext)
return;
textureUpdater()->updateTextureRect(sharedContext, sharedGrContext, resourceProvider, texture(), sourceRect, destOffset);
}
PassRefPtr<FrameBufferSkPictureCanvasLayerTextureUpdater> FrameBufferSkPictureCanvasLayerTextureUpdater::create(PassOwnPtr<LayerPainterChromium> painter)
{
return adoptRef(new FrameBufferSkPictureCanvasLayerTextureUpdater(painter));
}
FrameBufferSkPictureCanvasLayerTextureUpdater::FrameBufferSkPictureCanvasLayerTextureUpdater(PassOwnPtr<LayerPainterChromium> painter)
: SkPictureCanvasLayerTextureUpdater(painter)
{
}
FrameBufferSkPictureCanvasLayerTextureUpdater::~FrameBufferSkPictureCanvasLayerTextureUpdater()
{
}
PassOwnPtr<LayerTextureUpdater::Texture> FrameBufferSkPictureCanvasLayerTextureUpdater::createTexture(CCPrioritizedTextureManager* manager)
{
return adoptPtr(new Texture(this, CCPrioritizedTexture::create(manager)));
}
LayerTextureUpdater::SampledTexelFormat FrameBufferSkPictureCanvasLayerTextureUpdater::sampledTexelFormat(GC3Denum textureFormat)
{
return LayerTextureUpdater::SampledTexelFormatRGBA;
}
void FrameBufferSkPictureCanvasLayerTextureUpdater::updateTextureRect(WebGraphicsContext3D* context, GrContext* grContext, CCResourceProvider* resourceProvider, CCPrioritizedTexture* texture, const IntRect& sourceRect, const IntSize& destOffset)
{
texture->acquireBackingTexture(resourceProvider);
resourceProvider->flush();
CCResourceProvider::ScopedWriteLockGL lock(resourceProvider, texture->resourceId());
context->makeContextCurrent();
OwnPtr<SkCanvas> canvas = createAcceleratedCanvas(grContext, texture->size(), lock.textureId());
canvas->translate(0.0, texture->size().height());
canvas->scale(1.0, -1.0);
canvas->clipRect(SkRect::MakeXYWH(destOffset.width(), destOffset.height(), sourceRect.width(), sourceRect.height()));
canvas->translate(contentRect().x() - sourceRect.x() + destOffset.width(),
contentRect().y() - sourceRect.y() + destOffset.height());
drawPicture(canvas.get());
grContext->flush();
context->flush();
}
}
#endif