[Skia] Remove use of deprecated Skia constant and struct field names
[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
37 namespace WebCore {
38
39 static PassOwnPtr<SkCanvas> createAcceleratedCanvas(GraphicsContext3D* context,
40                                                     TextureAllocator* allocator,
41                                                     ManagedTexture* texture)
42 {
43     // Allocate so that we have a valid texture id.
44     texture->allocate(allocator);
45
46     GrContext* grContext = context->grContext();
47     IntSize canvasSize = texture->size();
48     GrPlatformTextureDesc textureDesc;
49     textureDesc.fFlags = kRenderTarget_GrPlatformTextureFlag;
50     textureDesc.fWidth = canvasSize.width();
51     textureDesc.fHeight = canvasSize.height();
52     textureDesc.fConfig = kSkia8888_PM_GrPixelConfig;
53     textureDesc.fTextureHandle = texture->textureId();
54     SkAutoTUnref<GrTexture> target(grContext->createPlatformTexture(textureDesc));
55     SkAutoTUnref<SkDevice> device(new SkGpuDevice(grContext, target.get()));
56     return adoptPtr(new SkCanvas(device.get()));
57 }
58
59 FrameBufferSkPictureCanvasLayerTextureUpdater::Texture::Texture(FrameBufferSkPictureCanvasLayerTextureUpdater* textureUpdater, PassOwnPtr<ManagedTexture> texture)
60     : LayerTextureUpdater::Texture(texture)
61     , m_textureUpdater(textureUpdater)
62 {
63 }
64
65 FrameBufferSkPictureCanvasLayerTextureUpdater::Texture::~Texture()
66 {
67 }
68
69 void FrameBufferSkPictureCanvasLayerTextureUpdater::Texture::updateRect(GraphicsContext3D* context, TextureAllocator* allocator, const IntRect& sourceRect, const IntRect& destRect)
70 {
71     textureUpdater()->updateTextureRect(context, allocator, texture(), sourceRect, destRect);
72 }
73
74 PassRefPtr<FrameBufferSkPictureCanvasLayerTextureUpdater> FrameBufferSkPictureCanvasLayerTextureUpdater::create(PassOwnPtr<LayerPainterChromium> painter)
75 {
76     return adoptRef(new FrameBufferSkPictureCanvasLayerTextureUpdater(painter));
77 }
78
79 FrameBufferSkPictureCanvasLayerTextureUpdater::FrameBufferSkPictureCanvasLayerTextureUpdater(PassOwnPtr<LayerPainterChromium> painter)
80     : SkPictureCanvasLayerTextureUpdater(painter)
81 {
82 }
83
84 FrameBufferSkPictureCanvasLayerTextureUpdater::~FrameBufferSkPictureCanvasLayerTextureUpdater()
85 {
86 }
87
88 PassOwnPtr<LayerTextureUpdater::Texture> FrameBufferSkPictureCanvasLayerTextureUpdater::createTexture(TextureManager* manager)
89 {
90     return adoptPtr(new Texture(this, ManagedTexture::create(manager)));
91 }
92
93 LayerTextureUpdater::SampledTexelFormat FrameBufferSkPictureCanvasLayerTextureUpdater::sampledTexelFormat(GC3Denum textureFormat)
94 {
95     // Here we directly render to the texture, so the component order is always correct.
96     return LayerTextureUpdater::SampledTexelFormatRGBA;
97 }
98
99 void FrameBufferSkPictureCanvasLayerTextureUpdater::updateTextureRect(GraphicsContext3D* context, TextureAllocator* allocator, ManagedTexture* texture, const IntRect& sourceRect, const IntRect& destRect)
100 {
101     // Make sure ganesh uses the correct GL context.
102     context->makeContextCurrent();
103     // Notify ganesh to sync its internal GL state.
104     context->grContext()->resetContext();
105
106     // Create an accelerated canvas to draw on.
107     OwnPtr<SkCanvas> canvas = createAcceleratedCanvas(context, allocator, texture);
108
109     // The compositor expects the textures to be upside-down so it can flip
110     // the final composited image. Ganesh renders the image upright so we
111     // need to do a y-flip.
112     canvas->translate(0.0, texture->size().height());
113     canvas->scale(1.0, -1.0);
114     // Only the region corresponding to destRect on the texture must be updated.
115     canvas->clipRect(SkRect(destRect));
116     // Translate the origin of contentRect to that of destRect.
117     // Note that destRect is defined relative to sourceRect.
118     canvas->translate(contentRect().x() - sourceRect.x() + destRect.x(),
119                       contentRect().y() - sourceRect.y() + destRect.y());
120     drawPicture(canvas.get());
121
122     // Flush ganesh context so that all the rendered stuff appears on the texture.
123     context->grContext()->flush();
124 }
125
126 } // namespace WebCore
127 #endif // USE(ACCELERATED_COMPOSITING)