initial import
[vuplus_webkit] / Source / WebCore / platform / graphics / chromium / DrawingBufferChromium.cpp
1 /*
2  * Copyright (c) 2010, 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 are
6  * met:
7  *
8  *     * Redistributions of source code must retain the above copyright
9  * notice, this list of conditions and the following disclaimer.
10  *     * Redistributions in binary form must reproduce the above
11  * copyright notice, this list of conditions and the following disclaimer
12  * in the documentation and/or other materials provided with the
13  * distribution.
14  *     * Neither the name of Google Inc. nor the names of its
15  * contributors may be used to endorse or promote products derived from
16  * this software without specific prior written permission.
17  *
18  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
19  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
20  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
21  * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
22  * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
23  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
24  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
25  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
26  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
28  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29  */
30
31 #include "config.h"
32
33 #include "DrawingBuffer.h"
34
35 #include "GraphicsContext3D.h"
36
37 namespace WebCore {
38
39 static unsigned generateColorTexture(GraphicsContext3D* context, const IntSize& size)
40 {
41     unsigned offscreenColorTexture = context->createTexture();
42     if (!offscreenColorTexture)
43         return 0;
44
45     context->bindTexture(GraphicsContext3D::TEXTURE_2D, offscreenColorTexture);
46     context->texParameteri(GraphicsContext3D::TEXTURE_2D, GraphicsContext3D::TEXTURE_MAG_FILTER, GraphicsContext3D::NEAREST);
47     context->texParameteri(GraphicsContext3D::TEXTURE_2D, GraphicsContext3D::TEXTURE_MIN_FILTER, GraphicsContext3D::NEAREST);
48     context->texParameteri(GraphicsContext3D::TEXTURE_2D, GraphicsContext3D::TEXTURE_WRAP_S, GraphicsContext3D::CLAMP_TO_EDGE);
49     context->texParameteri(GraphicsContext3D::TEXTURE_2D, GraphicsContext3D::TEXTURE_WRAP_T, GraphicsContext3D::CLAMP_TO_EDGE);
50     context->texImage2DResourceSafe(GraphicsContext3D::TEXTURE_2D, 0, GraphicsContext3D::RGBA, size.width(), size.height(), 0, GraphicsContext3D::RGBA, GraphicsContext3D::UNSIGNED_BYTE);
51     context->framebufferTexture2D(GraphicsContext3D::FRAMEBUFFER, GraphicsContext3D::COLOR_ATTACHMENT0, GraphicsContext3D::TEXTURE_2D, offscreenColorTexture, 0);
52
53     return offscreenColorTexture;
54 }
55
56 DrawingBuffer::DrawingBuffer(GraphicsContext3D* context,
57                              const IntSize& size,
58                              bool multisampleExtensionSupported,
59                              bool packedDepthStencilExtensionSupported)
60     : m_context(context)
61     , m_size(-1, -1)
62     , m_multisampleExtensionSupported(multisampleExtensionSupported)
63     , m_packedDepthStencilExtensionSupported(packedDepthStencilExtensionSupported)
64     , m_fbo(0)
65     , m_colorBuffer(0)
66     , m_depthStencilBuffer(0)
67     , m_depthBuffer(0)
68     , m_stencilBuffer(0)
69     , m_multisampleFBO(0)
70     , m_multisampleColorBuffer(0)
71 {
72     m_fbo = context->createFramebuffer();
73     context->bindFramebuffer(GraphicsContext3D::FRAMEBUFFER, m_fbo);
74     m_colorBuffer = generateColorTexture(context, size);
75     createSecondaryBuffers();
76     if (!reset(size)) {
77         m_context.clear();
78         return;
79     }
80 }
81
82 DrawingBuffer::~DrawingBuffer()
83 {
84     if (!m_context)
85         return;
86
87     clear();
88 }
89
90 #if USE(ACCELERATED_COMPOSITING)
91 void DrawingBuffer::publishToPlatformLayer()
92 {
93     if (!m_context)
94         return;
95
96     if (multisample())
97         commit();
98     m_context->makeContextCurrent();
99     m_context->flush();
100 }
101 #endif
102
103 #if USE(ACCELERATED_COMPOSITING)
104 PlatformLayer* DrawingBuffer::platformLayer()
105 {
106     return 0;
107 }
108 #endif
109
110 Platform3DObject DrawingBuffer::platformColorBuffer() const
111 {
112     return m_colorBuffer;
113 }
114
115 }