4d20326790b51e356bd530a78fd8b9296da59893
[WebKit-https.git] / Source / WebCore / platform / graphics / blackberry / CanvasLayerWebKitThread.cpp
1 /*
2  * Copyright (C) 2010, 2011, 2012 Research In Motion Limited. All rights reserved.
3  *
4  * This library is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU Lesser General Public
6  * License as published by the Free Software Foundation; either
7  * version 2 of the License, or (at your option) any later version.
8  *
9  * This library is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
12  * Lesser General Public License for more details.
13  *
14  * You should have received a copy of the GNU Lesser General Public
15  * License along with this library; if not, write to the Free Software
16  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
17  */
18
19 #include "config.h"
20 #include "CanvasLayerWebKitThread.h"
21
22 #if USE(ACCELERATED_COMPOSITING) && ENABLE(SKIA_GPU_CANVAS)
23
24 #include "HTMLCanvasElement.h"
25 #include "PlatformContextSkia.h"
26
27 #include <BlackBerryPlatformGraphics.h>
28 #include <GLES2/gl2.h>
29 #include <GrGLTexture.h>
30 #include <SkGpuDevice.h>
31
32 namespace WebCore {
33
34 CanvasLayerWebKitThread::CanvasLayerWebKitThread(HTMLCanvasElement* canvas)
35     : LayerWebKitThread(Layer, 0)
36 {
37     setCanvas(canvas);
38 }
39
40 CanvasLayerWebKitThread::~CanvasLayerWebKitThread()
41 {
42     if (m_texID) {
43         BlackBerry::Platform::Graphics::makeSharedResourceContextCurrent(BlackBerry::Platform::Graphics::GLES2);
44         glDeleteTextures(1, &m_texID);
45     }
46 }
47
48 void CanvasLayerWebKitThread::setCanvas(HTMLCanvasElement* canvas)
49 {
50     m_canvas = canvas;
51     setNeedsTexture(isDrawable() && canvas);
52     setLayerProgramShader(LayerProgramShaderRGBA);
53     setNeedsDisplay();
54 }
55
56 void CanvasLayerWebKitThread::setNeedsDisplay()
57 {
58     m_needsDisplay = true;
59     setNeedsCommit();
60 }
61
62 void CanvasLayerWebKitThread::updateTextureContentsIfNeeded()
63 {
64     if (!m_needsDisplay)
65         return;
66
67     m_needsDisplay = false;
68
69     if (!m_canvas || !m_canvas->drawingContext())
70         return;
71
72     SkGpuDevice* gpuDevice = static_cast<SkGpuDevice*>(m_canvas->drawingContext()->platformContext()->canvas()->getDevice());
73     gpuDevice->makeRenderTargetCurrent();
74     // We know this is a GrGLTexture
75     if (GrGLTexture* deviceTexture = (GrGLTexture*)gpuDevice->accessTexture()) {
76         m_texWidth = deviceTexture->allocWidth();
77         m_texHeight = deviceTexture->allocHeight();
78     }
79
80     GLint previousTexture;
81     glGetIntegerv(GL_TEXTURE_BINDING_2D, &previousTexture);
82
83     if (!m_texID) {
84         glGenTextures(1, &m_texID);
85         glBindTexture(GL_TEXTURE_2D, m_texID);
86         glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
87         glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
88         glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
89         glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
90         glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, m_canvas->width(), m_canvas->height(), 0, GL_RGBA, GL_UNSIGNED_BYTE, 0);
91
92         createFrontBufferLock();
93     }
94
95     pthread_mutex_lock(m_frontBufferLock);
96     glBindTexture(GL_TEXTURE_2D, m_texID);
97     glCopyTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, 0, 0, m_texWidth, m_texHeight, 0);
98     glBindTexture(GL_TEXTURE_2D, previousTexture);
99     pthread_mutex_unlock(m_frontBufferLock);
100 }
101
102 }
103
104 #endif // USE(ACCELERATED_COMPOSITING) && ENABLE(SKIA_GPU_CANVAS)