1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
|
/*
* Copyright (C) 2010 Google Inc. All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are
* met:
*
* * Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* * Redistributions in binary form must reproduce the above
* copyright notice, this list of conditions and the following disclaimer
* in the documentation and/or other materials provided with the
* distribution.
* * Neither the name of Google Inc. nor the names of its
* contributors may be used to endorse or promote products derived from
* this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
* OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
#include "config.h"
#if USE(ACCELERATED_COMPOSITING)
#include "ContentLayerChromium.h"
#include "LayerRendererChromium.h"
#include "RenderLayerBacking.h"
#if PLATFORM(SKIA)
#include "NativeImageSkia.h"
#include "PlatformContextSkia.h"
#include "skia/ext/platform_canvas.h"
#elif PLATFORM(CG)
#include <CoreGraphics/CGBitmapContext.h>
#endif
#include <GLES2/gl2.h>
namespace WebCore {
ContentLayerChromium::SharedValues::SharedValues()
: m_contentShaderProgram(0)
, m_shaderSamplerLocation(-1)
, m_shaderMatrixLocation(-1)
, m_shaderAlphaLocation(-1)
, m_initialized(false)
{
// Shaders for drawing the layer contents.
char vertexShaderString[] =
"attribute vec4 a_position; \n"
"attribute vec2 a_texCoord; \n"
"uniform mat4 matrix; \n"
"varying vec2 v_texCoord; \n"
"void main() \n"
"{ \n"
" gl_Position = matrix * a_position; \n"
" v_texCoord = a_texCoord; \n"
"} \n";
// Note differences between Skia and Core Graphics versions:
// - Skia uses BGRA and origin is upper left
// - Core Graphics uses RGBA and origin is lower left
char fragmentShaderString[] =
"precision mediump float; \n"
"varying vec2 v_texCoord; \n"
"uniform sampler2D s_texture; \n"
"uniform float alpha; \n"
"void main() \n"
"{ \n"
#if PLATFORM(SKIA)
" vec4 texColor = texture2D(s_texture, v_texCoord); \n"
" gl_FragColor = vec4(texColor.z, texColor.y, texColor.x, texColor.w) * alpha; \n"
#elif PLATFORM(CG)
" vec4 texColor = texture2D(s_texture, vec2(v_texCoord.x, 1.0 - v_texCoord.y)); \n"
" gl_FragColor = vec4(texColor.x, texColor.y, texColor.z, texColor.w) * alpha; \n"
#else
#error "Need to implement for your platform."
#endif
"} \n";
m_contentShaderProgram = createShaderProgram(vertexShaderString, fragmentShaderString);
if (!m_contentShaderProgram) {
LOG_ERROR("ContentLayerChromium: Failed to create shader program");
return;
}
m_shaderSamplerLocation = glGetUniformLocation(m_contentShaderProgram, "s_texture");
m_shaderMatrixLocation = glGetUniformLocation(m_contentShaderProgram, "matrix");
m_shaderAlphaLocation = glGetUniformLocation(m_contentShaderProgram, "alpha");
ASSERT(m_shaderSamplerLocation != -1);
ASSERT(m_shaderMatrixLocation != -1);
ASSERT(m_shaderAlphaLocation != -1);
m_initialized = true;
}
ContentLayerChromium::SharedValues::~SharedValues()
{
if (m_contentShaderProgram)
GLC(glDeleteProgram(m_contentShaderProgram));
}
PassRefPtr<ContentLayerChromium> ContentLayerChromium::create(GraphicsLayerChromium* owner)
{
return adoptRef(new ContentLayerChromium(owner));
}
ContentLayerChromium::ContentLayerChromium(GraphicsLayerChromium* owner)
: LayerChromium(owner)
, m_contentsTexture(0)
{
}
ContentLayerChromium::~ContentLayerChromium()
{
if (m_contentsTexture)
GLC(glDeleteTextures(1, &m_contentsTexture));
}
void ContentLayerChromium::updateContents()
{
RenderLayerBacking* backing = static_cast<RenderLayerBacking*>(m_owner->client());
if (!backing || backing->paintingGoesToWindow())
return;
ASSERT(drawsContent());
ASSERT(layerRenderer());
// FIXME: Remove this test when tiled layers are implemented.
m_skipsDraw = false;
if (!layerRenderer()->checkTextureSize(m_bounds)) {
m_skipsDraw = true;
return;
}
void* pixels = 0;
IntRect dirtyRect(m_dirtyRect);
IntSize requiredTextureSize;
IntSize bitmapSize;
#if PLATFORM(SKIA)
const SkBitmap* skiaBitmap = 0;
OwnPtr<skia::PlatformCanvas> canvas;
OwnPtr<PlatformContextSkia> skiaContext;
OwnPtr<GraphicsContext> graphicsContext;
requiredTextureSize = m_bounds;
IntRect boundsRect(IntPoint(0, 0), m_bounds);
// If the texture needs to be reallocated then we must redraw the entire
// contents of the layer.
if (requiredTextureSize != m_allocatedTextureSize)
dirtyRect = boundsRect;
else {
// Clip the dirtyRect to the size of the layer to avoid drawing outside
// the bounds of the backing texture.
dirtyRect.intersect(boundsRect);
}
canvas.set(new skia::PlatformCanvas(dirtyRect.width(), dirtyRect.height(), false));
skiaContext.set(new PlatformContextSkia(canvas.get()));
#if OS(WINDOWS)
// This is needed to get text to show up correctly. Without it,
// GDI renders with zero alpha and the text becomes invisible.
// Unfortunately, setting this to true disables cleartype.
// FIXME: Does this take us down a very slow text rendering path?
// FIXME: why is this is a windows-only call ?
skiaContext->setDrawingToImageBuffer(true);
#endif
graphicsContext.set(new GraphicsContext(reinterpret_cast<PlatformGraphicsContext*>(skiaContext.get())));
// Bring the canvas into the coordinate system of the paint rect.
canvas->translate(static_cast<SkScalar>(-dirtyRect.x()), static_cast<SkScalar>(-dirtyRect.y()));
m_owner->paintGraphicsLayerContents(*graphicsContext, dirtyRect);
const SkBitmap& bitmap = canvas->getDevice()->accessBitmap(false);
skiaBitmap = &bitmap;
ASSERT(skiaBitmap);
SkAutoLockPixels lock(*skiaBitmap);
SkBitmap::Config skiaConfig = skiaBitmap->config();
// FIXME: do we need to support more image configurations?
if (skiaConfig == SkBitmap::kARGB_8888_Config) {
pixels = skiaBitmap->getPixels();
bitmapSize = IntSize(skiaBitmap->width(), skiaBitmap->height());
}
#elif PLATFORM(CG)
requiredTextureSize = m_bounds;
IntRect boundsRect(IntPoint(0, 0), m_bounds);
// If the texture needs to be reallocated then we must redraw the entire
// contents of the layer.
if (requiredTextureSize != m_allocatedTextureSize)
dirtyRect = boundsRect;
else {
// Clip the dirtyRect to the size of the layer to avoid drawing outside
// the bounds of the backing texture.
dirtyRect.intersect(boundsRect);
}
Vector<uint8_t> tempVector;
int rowBytes = 4 * dirtyRect.width();
tempVector.resize(rowBytes * dirtyRect.height());
memset(tempVector.data(), 0, tempVector.size());
RetainPtr<CGColorSpaceRef> colorSpace(AdoptCF, CGColorSpaceCreateDeviceRGB());
RetainPtr<CGContextRef> contextCG(AdoptCF, CGBitmapContextCreate(tempVector.data(),
dirtyRect.width(), dirtyRect.height(), 8, rowBytes,
colorSpace.get(),
kCGImageAlphaPremultipliedLast));
GraphicsContext graphicsContext(contextCG.get());
// Translate the graphics contxt into the coordinate system of the dirty rect.
graphicsContext.translate(-dirtyRect.x(), -dirtyRect.y());
m_owner->paintGraphicsLayerContents(graphicsContext, dirtyRect);
pixels = tempVector.data();
bitmapSize = dirtyRect.size();
#else
#error "Need to implement for your platform."
#endif
unsigned textureId = m_contentsTexture;
if (!textureId)
textureId = layerRenderer()->createLayerTexture();
if (pixels)
updateTextureRect(pixels, bitmapSize, requiredTextureSize, dirtyRect, textureId);
}
void ContentLayerChromium::updateTextureRect(void* pixels, const IntSize& bitmapSize, const IntSize& requiredTextureSize, const IntRect& updateRect, unsigned textureId)
{
if (!pixels)
return;
glBindTexture(GL_TEXTURE_2D, textureId);
// If the texture id or size changed since last time then we need to tell GL
// to re-allocate a texture.
if (m_contentsTexture != textureId || requiredTextureSize != m_allocatedTextureSize) {
ASSERT(bitmapSize == requiredTextureSize);
GLC(glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, requiredTextureSize.width(), requiredTextureSize.height(), 0, GL_RGBA, GL_UNSIGNED_BYTE, pixels));
m_contentsTexture = textureId;
m_allocatedTextureSize = requiredTextureSize;
} else {
ASSERT(updateRect.width() <= m_allocatedTextureSize.width() && updateRect.height() <= m_allocatedTextureSize.height());
ASSERT(updateRect.width() == bitmapSize.width() && updateRect.height() == bitmapSize.height());
#if PLATFORM(CG)
// The origin is at the lower left in Core Graphics' coordinate system. We need to correct for this here.
GLC(glTexSubImage2D(GL_TEXTURE_2D, 0,
updateRect.x(), m_allocatedTextureSize.height() - updateRect.height() - updateRect.y(),
updateRect.width(), updateRect.height(),
GL_RGBA, GL_UNSIGNED_BYTE, pixels));
#elif PLATFORM(SKIA)
GLC(glTexSubImage2D(GL_TEXTURE_2D, 0, updateRect.x(), updateRect.y(), updateRect.width(), updateRect.height(), GL_RGBA, GL_UNSIGNED_BYTE, pixels));
#else
#error "Need to implement for your platform."
#endif
}
m_dirtyRect.setSize(FloatSize());
m_contentsDirty = false;
}
void ContentLayerChromium::draw()
{
if (m_skipsDraw)
return;
ASSERT(layerRenderer());
const ContentLayerChromium::SharedValues* sv = layerRenderer()->contentLayerSharedValues();
ASSERT(sv && sv->initialized());
GLC(glActiveTexture(GL_TEXTURE0));
GLC(glBindTexture(GL_TEXTURE_2D, m_contentsTexture));
layerRenderer()->useShader(sv->contentShaderProgram());
GLC(glUniform1i(sv->shaderSamplerLocation(), 0));
drawTexturedQuad(layerRenderer()->projectionMatrix(), drawTransform(),
bounds().width(), bounds().height(), drawOpacity(),
sv->shaderMatrixLocation(), sv->shaderAlphaLocation());
}
}
#endif // USE(ACCELERATED_COMPOSITING)
|