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
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
|
/*
* 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 "LayerChromium.h"
#include "LayerRendererChromium.h"
#if PLATFORM(SKIA)
#include "NativeImageSkia.h"
#include "PlatformContextSkia.h"
#endif
#include "RenderLayerBacking.h"
#include "skia/ext/platform_canvas.h"
#include <GLES2/gl2.h>
namespace WebCore {
using namespace std;
const unsigned LayerChromium::s_positionAttribLocation = 0;
const unsigned LayerChromium::s_texCoordAttribLocation = 1;
static GLuint loadShader(GLenum type, const char* shaderSource)
{
GLuint shader = glCreateShader(type);
if (!shader)
return 0;
GLC(glShaderSource(shader, 1, &shaderSource, 0));
GLC(glCompileShader(shader));
GLint compiled;
GLC(glGetShaderiv(shader, GL_COMPILE_STATUS, &compiled));
if (!compiled) {
GLC(glDeleteShader(shader));
return 0;
}
return shader;
}
LayerChromium::SharedValues::SharedValues()
: m_quadVerticesVbo(0)
, m_quadElementsVbo(0)
, m_maxTextureSize(0)
, m_borderShaderProgram(0)
, m_borderShaderMatrixLocation(-1)
, m_borderShaderColorLocation(-1)
, m_initialized(false)
{
// Vertex positions and texture coordinates for the 4 corners of a 1x1 quad.
GLfloat vertices[] = { -0.5f, 0.5f, 0.0f, 0.0f, 1.0f,
-0.5f, -0.5f, 0.0f, 0.0f, 0.0f,
0.5f, -0.5f, 0.0f, 1.0f, 0.0f,
0.5f, 0.5f, 0.0f, 1.0f, 1.0f };
GLushort indices[] = { 0, 1, 2, 0, 2, 3, // The two triangles that make up the layer quad.
0, 1, 2, 3}; // A line path for drawing the layer border.
GLuint vboIds[2];
GLC(glGenBuffers(2, vboIds));
m_quadVerticesVbo = vboIds[0];
m_quadElementsVbo = vboIds[1];
GLC(glBindBuffer(GL_ARRAY_BUFFER, m_quadVerticesVbo));
GLC(glBufferData(GL_ARRAY_BUFFER, sizeof(vertices), vertices, GL_STATIC_DRAW));
GLC(glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, m_quadElementsVbo));
GLC(glBufferData(GL_ELEMENT_ARRAY_BUFFER, sizeof(indices), indices, GL_STATIC_DRAW));
// Get the max texture size supported by the system.
GLC(glGetIntegerv(GL_MAX_TEXTURE_SIZE, &m_maxTextureSize));
// Shaders for drawing the debug borders around the layers.
char borderVertexShaderString[] =
"attribute vec4 a_position; \n"
"uniform mat4 matrix; \n"
"void main() \n"
"{ \n"
" gl_Position = matrix * a_position; \n"
"} \n";
char borderFragmentShaderString[] =
"precision mediump float; \n"
"uniform vec4 color; \n"
"void main() \n"
"{ \n"
" gl_FragColor = color; \n"
"} \n";
m_borderShaderProgram = createShaderProgram(borderVertexShaderString, borderFragmentShaderString);
if (!m_borderShaderProgram) {
LOG_ERROR("ContentLayerChromium: Failed to create shader program");
return;
}
m_borderShaderMatrixLocation = glGetUniformLocation(m_borderShaderProgram, "matrix");
m_borderShaderColorLocation = glGetUniformLocation(m_borderShaderProgram, "color");
ASSERT(m_borderShaderMatrixLocation != -1);
ASSERT(m_borderShaderColorLocation != -1);
m_initialized = true;
}
LayerChromium::SharedValues::~SharedValues()
{
GLuint vboIds[2] = { m_quadVerticesVbo, m_quadElementsVbo };
GLC(glDeleteBuffers(2, vboIds));
if (m_borderShaderProgram)
GLC(glDeleteProgram(m_borderShaderProgram));
}
PassRefPtr<LayerChromium> LayerChromium::create(GraphicsLayerChromium* owner)
{
return adoptRef(new LayerChromium(owner));
}
LayerChromium::LayerChromium(GraphicsLayerChromium* owner)
: m_owner(owner)
, m_contentsDirty(false)
, m_superlayer(0)
, m_anchorPoint(0.5, 0.5)
, m_backgroundColor(0, 0, 0, 0)
, m_borderColor(0, 0, 0, 0)
, m_opacity(1.0)
, m_zPosition(0.0)
, m_anchorPointZ(0)
, m_borderWidth(0)
, m_clearsContext(false)
, m_doubleSided(true)
, m_hidden(false)
, m_masksToBounds(false)
, m_opaque(true)
, m_geometryFlipped(false)
, m_needsDisplayOnBoundsChange(false)
, m_layerRenderer(0)
{
}
LayerChromium::~LayerChromium()
{
// Our superlayer should be holding a reference to us so there should be no
// way for us to be destroyed while we still have a superlayer.
ASSERT(!superlayer());
// Remove the superlayer reference from all sublayers.
removeAllSublayers();
}
void LayerChromium::setLayerRenderer(LayerRendererChromium* renderer)
{
// It's not expected that layers will ever switch renderers.
ASSERT(!renderer || !m_layerRenderer || renderer == m_layerRenderer);
m_layerRenderer = renderer;
}
unsigned LayerChromium::createShaderProgram(const char* vertexShaderSource, const char* fragmentShaderSource)
{
GLuint vertexShader = loadShader(GL_VERTEX_SHADER, vertexShaderSource);
if (!vertexShader) {
LOG_ERROR("Failed to create vertex shader");
return 0;
}
GLuint fragmentShader = loadShader(GL_FRAGMENT_SHADER, fragmentShaderSource);
if (!fragmentShader) {
GLC(glDeleteShader(vertexShader));
LOG_ERROR("Failed to create fragment shader");
return 0;
}
GLuint programObject = glCreateProgram();
if (!programObject) {
LOG_ERROR("Failed to create shader program");
return 0;
}
GLC(glAttachShader(programObject, vertexShader));
GLC(glAttachShader(programObject, fragmentShader));
// Bind the common attrib locations.
GLC(glBindAttribLocation(programObject, s_positionAttribLocation, "a_position"));
GLC(glBindAttribLocation(programObject, s_texCoordAttribLocation, "a_texCoord"));
GLC(glLinkProgram(programObject));
GLint linked;
GLC(glGetProgramiv(programObject, GL_LINK_STATUS, &linked));
if (!linked) {
LOG_ERROR("Failed to link shader program");
GLC(glDeleteProgram(programObject));
return 0;
}
GLC(glDeleteShader(vertexShader));
GLC(glDeleteShader(fragmentShader));
return programObject;
}
void LayerChromium::setNeedsCommit()
{
// Call notifySyncRequired(), which in this implementation plumbs through to
// call setRootLayerNeedsDisplay() on the WebView, which will cause LayerRendererChromium
// to render a frame.
if (m_owner)
m_owner->notifySyncRequired();
}
void LayerChromium::addSublayer(PassRefPtr<LayerChromium> sublayer)
{
insertSublayer(sublayer, numSublayers());
}
void LayerChromium::insertSublayer(PassRefPtr<LayerChromium> sublayer, size_t index)
{
index = min(index, m_sublayers.size());
sublayer->removeFromSuperlayer();
sublayer->setSuperlayer(this);
m_sublayers.insert(index, sublayer);
setNeedsCommit();
}
void LayerChromium::removeFromSuperlayer()
{
if (m_superlayer)
m_superlayer->removeSublayer(this);
}
void LayerChromium::removeSublayer(LayerChromium* sublayer)
{
int foundIndex = indexOfSublayer(sublayer);
if (foundIndex == -1)
return;
sublayer->setSuperlayer(0);
m_sublayers.remove(foundIndex);
setNeedsCommit();
}
void LayerChromium::replaceSublayer(LayerChromium* reference, PassRefPtr<LayerChromium> newLayer)
{
ASSERT_ARG(reference, reference);
ASSERT_ARG(reference, reference->superlayer() == this);
if (reference == newLayer)
return;
int referenceIndex = indexOfSublayer(reference);
if (referenceIndex == -1) {
ASSERT_NOT_REACHED();
return;
}
reference->removeFromSuperlayer();
if (newLayer) {
newLayer->removeFromSuperlayer();
insertSublayer(newLayer, referenceIndex);
}
}
int LayerChromium::indexOfSublayer(const LayerChromium* reference)
{
for (size_t i = 0; i < m_sublayers.size(); i++) {
if (m_sublayers[i] == reference)
return i;
}
return -1;
}
void LayerChromium::setBounds(const IntSize& size)
{
if (m_bounds == size)
return;
m_bounds = size;
m_backingStoreSize = size;
setNeedsCommit();
}
void LayerChromium::setFrame(const FloatRect& rect)
{
if (rect == m_frame)
return;
m_frame = rect;
setNeedsCommit();
}
const LayerChromium* LayerChromium::rootLayer() const
{
const LayerChromium* layer = this;
for (LayerChromium* superlayer = layer->superlayer(); superlayer; layer = superlayer, superlayer = superlayer->superlayer()) { }
return layer;
}
void LayerChromium::removeAllSublayers()
{
while (m_sublayers.size()) {
LayerChromium* layer = m_sublayers[0].get();
ASSERT(layer->superlayer());
layer->removeFromSuperlayer();
}
}
void LayerChromium::setSublayers(const Vector<RefPtr<LayerChromium> >& sublayers)
{
if (sublayers == m_sublayers)
return;
removeAllSublayers();
size_t listSize = sublayers.size();
for (size_t i = 0; i < listSize; i++)
addSublayer(sublayers[i]);
}
LayerChromium* LayerChromium::superlayer() const
{
return m_superlayer;
}
void LayerChromium::setNeedsDisplay(const FloatRect& dirtyRect)
{
// Simply mark the contents as dirty. The actual redraw will
// happen when it's time to do the compositing.
m_contentsDirty = true;
m_dirtyRect.unite(dirtyRect);
setNeedsCommit();
}
void LayerChromium::setNeedsDisplay()
{
m_dirtyRect.setSize(m_bounds);
m_contentsDirty = true;
}
void LayerChromium::toGLMatrix(float* flattened, const TransformationMatrix& m)
{
flattened[0] = m.m11();
flattened[1] = m.m12();
flattened[2] = m.m13();
flattened[3] = m.m14();
flattened[4] = m.m21();
flattened[5] = m.m22();
flattened[6] = m.m23();
flattened[7] = m.m24();
flattened[8] = m.m31();
flattened[9] = m.m32();
flattened[10] = m.m33();
flattened[11] = m.m34();
flattened[12] = m.m41();
flattened[13] = m.m42();
flattened[14] = m.m43();
flattened[15] = m.m44();
}
void LayerChromium::drawTexturedQuad(const TransformationMatrix& projectionMatrix, const TransformationMatrix& drawMatrix,
float width, float height, float opacity,
int matrixLocation, int alphaLocation)
{
static GLfloat glMatrix[16];
TransformationMatrix renderMatrix = drawMatrix;
// Apply a scaling factor to size the quad from 1x1 to its intended size.
renderMatrix.scale3d(width, height, 1);
// Apply the projection matrix before sending the transform over to the shader.
renderMatrix.multiply(projectionMatrix);
toGLMatrix(&glMatrix[0], renderMatrix);
GLC(glUniformMatrix4fv(matrixLocation, 1, false, &glMatrix[0]));
if (alphaLocation != -1)
GLC(glUniform1f(alphaLocation, opacity));
GLC(glDrawElements(GL_TRIANGLES, 6, GL_UNSIGNED_SHORT, 0));
}
void LayerChromium::drawDebugBorder()
{
static GLfloat glMatrix[16];
if (!borderColor().alpha())
return;
ASSERT(layerRenderer());
const SharedValues* sv = layerRenderer()->layerSharedValues();
ASSERT(sv && sv->initialized());
layerRenderer()->useShader(sv->borderShaderProgram());
TransformationMatrix renderMatrix = drawTransform();
renderMatrix.scale3d(bounds().width(), bounds().height(), 1);
renderMatrix.multiply(layerRenderer()->projectionMatrix());
toGLMatrix(&glMatrix[0], renderMatrix);
GLC(glUniformMatrix4fv(sv->borderShaderMatrixLocation(), 1, false, &glMatrix[0]));
GLC(glUniform4f(sv->borderShaderColorLocation(), borderColor().red() / 255.0, borderColor().green() / 255.0, borderColor().blue() / 255.0, 1));
GLC(glLineWidth(borderWidth()));
// The indices for the line are stored in the same array as the triangle indices.
GLC(glDrawElements(GL_LINE_LOOP, 4, GL_UNSIGNED_SHORT, (void*)(6 * sizeof(unsigned short))));
}
// static
void LayerChromium::prepareForDraw(const SharedValues* sv)
{
GLC(glBindBuffer(GL_ARRAY_BUFFER, sv->quadVerticesVbo()));
GLC(glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, sv->quadElementsVbo()));
GLuint offset = 0;
GLC(glVertexAttribPointer(s_positionAttribLocation, 3, GL_FLOAT, GL_FALSE, 5 * sizeof(GLfloat), (GLvoid*)offset));
offset += 3 * sizeof(GLfloat);
GLC(glVertexAttribPointer(s_texCoordAttribLocation, 2, GL_FLOAT, GL_FALSE, 5 * sizeof(GLfloat), (GLvoid*)offset));
GLC(glEnableVertexAttribArray(s_positionAttribLocation));
GLC(glEnableVertexAttribArray(s_texCoordAttribLocation));
}
}
#endif // USE(ACCELERATED_COMPOSITING)
|