[chromium] Replace destRect with destOffset in texture upload
[WebKit-https.git] / Source / WebCore / platform / graphics / chromium / FrameBufferSkPictureCanvasLayerTextureUpdater.cpp
1 /*
2  * Copyright (C) 2011 Google Inc. All rights reserved.
3  *
4  * Redistribution and use in source and binary forms, with or without
5  * modification, are permitted provided that the following conditions
6  * are met:
7  *
8  * 1.  Redistributions of source code must retain the above copyright
9  *     notice, this list of conditions and the following disclaimer.
10  * 2.  Redistributions in binary form must reproduce the above copyright
11  *     notice, this list of conditions and the following disclaimer in the
12  *     documentation and/or other materials provided with the distribution.
13  *
14  * THIS SOFTWARE IS PROVIDED BY APPLE AND ITS CONTRIBUTORS "AS IS" AND ANY
15  * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
16  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
17  * DISCLAIMED. IN NO EVENT SHALL APPLE OR ITS CONTRIBUTORS BE LIABLE FOR ANY
18  * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
19  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
20  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
21  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
22  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
23  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
24  */
25
26
27 #include "config.h"
28
29 #if USE(ACCELERATED_COMPOSITING)
30
31 #include "FrameBufferSkPictureCanvasLayerTextureUpdater.h"
32
33 #include "LayerPainterChromium.h"
34 #include "SkCanvas.h"
35 #include "SkGpuDevice.h"
36 #include "cc/CCProxy.h"
37 #include <public/WebGraphicsContext3D.h>
38 #include <public/WebSharedGraphicsContext3D.h>
39
40 using WebKit::WebGraphicsContext3D;
41 using WebKit::WebSharedGraphicsContext3D;
42
43 namespace WebCore {
44
45 static PassOwnPtr<SkCanvas> createAcceleratedCanvas(GrContext* grContext,
46                                                     IntSize canvasSize,
47                                                     unsigned textureId)
48 {
49     GrPlatformTextureDesc textureDesc;
50     textureDesc.fFlags = kRenderTarget_GrPlatformTextureFlag;
51     textureDesc.fWidth = canvasSize.width();
52     textureDesc.fHeight = canvasSize.height();
53     textureDesc.fConfig = kSkia8888_PM_GrPixelConfig;
54     textureDesc.fTextureHandle = textureId;
55     SkAutoTUnref<GrTexture> target(grContext->createPlatformTexture(textureDesc));
56     SkAutoTUnref<SkDevice> device(new SkGpuDevice(grContext, target.get()));
57     return adoptPtr(new SkCanvas(device.get()));
58 }
59
60 FrameBufferSkPictureCanvasLayerTextureUpdater::Texture::Texture(FrameBufferSkPictureCanvasLayerTextureUpdater* textureUpdater, PassOwnPtr<CCPrioritizedTexture> texture)
61     : LayerTextureUpdater::Texture(texture)
62     , m_textureUpdater(textureUpdater)
63 {
64 }
65
66 FrameBufferSkPictureCanvasLayerTextureUpdater::Texture::~Texture()
67 {
68 }
69
70 void FrameBufferSkPictureCanvasLayerTextureUpdater::Texture::updateRect(CCResourceProvider* resourceProvider, const IntRect& sourceRect, const IntSize& destOffset)
71 {
72     WebGraphicsContext3D* sharedContext = CCProxy::hasImplThread() ? WebSharedGraphicsContext3D::compositorThreadContext() : WebSharedGraphicsContext3D::mainThreadContext();
73     GrContext* sharedGrContext = CCProxy::hasImplThread() ? WebSharedGraphicsContext3D::compositorThreadGrContext() : WebSharedGraphicsContext3D::mainThreadGrContext();
74     if (!sharedContext || !sharedGrContext)
75         return;
76     textureUpdater()->updateTextureRect(sharedContext, sharedGrContext, resourceProvider, texture(), sourceRect, destOffset);
77 }
78
79 PassRefPtr<FrameBufferSkPictureCanvasLayerTextureUpdater> FrameBufferSkPictureCanvasLayerTextureUpdater::create(PassOwnPtr<LayerPainterChromium> painter)
80 {
81     return adoptRef(new FrameBufferSkPictureCanvasLayerTextureUpdater(painter));
82 }
83
84 FrameBufferSkPictureCanvasLayerTextureUpdater::FrameBufferSkPictureCanvasLayerTextureUpdater(PassOwnPtr<LayerPainterChromium> painter)
85     : SkPictureCanvasLayerTextureUpdater(painter)
86 {
87 }
88
89 FrameBufferSkPictureCanvasLayerTextureUpdater::~FrameBufferSkPictureCanvasLayerTextureUpdater()
90 {
91 }
92
93 PassOwnPtr<LayerTextureUpdater::Texture> FrameBufferSkPictureCanvasLayerTextureUpdater::createTexture(CCPrioritizedTextureManager* manager)
94 {
95     return adoptPtr(new Texture(this, CCPrioritizedTexture::create(manager)));
96 }
97
98 LayerTextureUpdater::SampledTexelFormat FrameBufferSkPictureCanvasLayerTextureUpdater::sampledTexelFormat(GC3Denum textureFormat)
99 {
100     // Here we directly render to the texture, so the component order is always correct.
101     return LayerTextureUpdater::SampledTexelFormatRGBA;
102 }
103
104 void FrameBufferSkPictureCanvasLayerTextureUpdater::updateTextureRect(WebGraphicsContext3D* context, GrContext* grContext, CCResourceProvider* resourceProvider, CCPrioritizedTexture* texture, const IntRect& sourceRect, const IntSize& destOffset)
105 {
106     // Make sure ganesh uses the correct GL context.
107     context->makeContextCurrent();
108
109     texture->acquireBackingTexture(resourceProvider);
110     CCScopedLockResourceForWrite lock(resourceProvider, texture->resourceId());
111     // Create an accelerated canvas to draw on.
112     OwnPtr<SkCanvas> canvas = createAcceleratedCanvas(grContext, texture->size(), lock.textureId());
113
114     // The compositor expects the textures to be upside-down so it can flip
115     // the final composited image. Ganesh renders the image upright so we
116     // need to do a y-flip.
117     canvas->translate(0.0, texture->size().height());
118     canvas->scale(1.0, -1.0);
119     // Clip to the destination on the texture that must be updated.
120     canvas->clipRect(SkRect::MakeXYWH(destOffset.width(), destOffset.height(), sourceRect.width(), sourceRect.height()));
121     // Translate the origin of contentRect to that of destRect.
122     // Note that destRect is defined relative to sourceRect.
123     canvas->translate(contentRect().x() - sourceRect.x() + destOffset.width(),
124                       contentRect().y() - sourceRect.y() + destOffset.height());
125     drawPicture(canvas.get());
126
127     // Flush ganesh context so that all the rendered stuff appears on the texture.
128     grContext->flush();
129
130     // Flush the GL context so rendering results from this context are visible in the compositor's context.
131     context->flush();
132 }
133
134 } // namespace WebCore
135 #endif // USE(ACCELERATED_COMPOSITING)