initial import
[vuplus_webkit] / Source / WebCore / platform / graphics / chromium / LayerRendererChromium.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
32 #include "config.h"
33
34 #if USE(ACCELERATED_COMPOSITING)
35 #include "LayerRendererChromium.h"
36
37 #include "Canvas2DLayerChromium.h"
38 #include "Extensions3DChromium.h"
39 #include "FloatQuad.h"
40 #include "GeometryBinding.h"
41 #include "GraphicsContext3D.h"
42 #include "LayerChromium.h"
43 #include "LayerPainterChromium.h"
44 #include "ManagedTexture.h"
45 #include "LayerTextureUpdaterCanvas.h"
46 #include "NonCompositedContentHost.h"
47 #include "NotImplemented.h"
48 #include "PlatformColor.h"
49 #include "RenderSurfaceChromium.h"
50 #include "TextStream.h"
51 #include "TextureManager.h"
52 #include "TreeSynchronizer.h"
53 #include "TraceEvent.h"
54 #include "WebGLLayerChromium.h"
55 #include "cc/CCLayerImpl.h"
56 #include "cc/CCLayerTreeHostCommon.h"
57 #include "cc/CCMainThreadTask.h"
58 #if USE(SKIA)
59 #include "Extensions3D.h"
60 #include "GrContext.h"
61 #include "NativeImageSkia.h"
62 #include "PlatformContextSkia.h"
63 #elif USE(CG)
64 #include <CoreGraphics/CGBitmapContext.h>
65 #endif
66 #include <wtf/CurrentTime.h>
67 #include <wtf/MainThread.h>
68
69 using namespace std;
70
71 namespace WebCore {
72
73 namespace {
74
75 static TransformationMatrix orthoMatrix(float left, float right, float bottom, float top)
76 {
77     float deltaX = right - left;
78     float deltaY = top - bottom;
79     TransformationMatrix ortho;
80     if (!deltaX || !deltaY)
81         return ortho;
82     ortho.setM11(2.0f / deltaX);
83     ortho.setM41(-(right + left) / deltaX);
84     ortho.setM22(2.0f / deltaY);
85     ortho.setM42(-(top + bottom) / deltaY);
86
87     // Z component of vertices is always set to zero as we don't use the depth buffer
88     // while drawing.
89     ortho.setM33(0);
90
91     return ortho;
92 }
93
94 static TransformationMatrix screenMatrix(int x, int y, int width, int height)
95 {
96     TransformationMatrix screen;
97
98     // Map to viewport.
99     screen.translate3d(x, y, 0);
100     screen.scale3d(width, height, 0);
101
102     // Map x, y and z to unit square.
103     screen.translate3d(0.5, 0.5, 0.5);
104     screen.scale3d(0.5, 0.5, 0.5);
105
106     return screen;
107 }
108
109 #if USE(SKIA)
110 bool contextSupportsAcceleratedPainting(GraphicsContext3D* context)
111 {
112     WebCore::Extensions3D* extensions = context->getExtensions();
113     if (extensions->supports("GL_EXT_texture_format_BGRA8888"))
114         extensions->ensureEnabled("GL_EXT_texture_format_BGRA8888");
115     else
116         return false;
117
118     if (extensions->supports("GL_EXT_read_format_bgra"))
119         extensions->ensureEnabled("GL_EXT_read_format_bgra");
120     else
121         return false;
122
123     if (!context->grContext())
124         return false;
125
126     return true;
127 }
128 #endif
129
130 } // anonymous namespace
131
132 PassOwnPtr<LayerRendererChromium> LayerRendererChromium::create(CCLayerTreeHostImpl* owner, PassRefPtr<GraphicsContext3D> context)
133 {
134 #if USE(SKIA)
135     if (owner->settings().acceleratePainting && !contextSupportsAcceleratedPainting(context.get()))
136         return nullptr;
137 #endif
138     OwnPtr<LayerRendererChromium> layerRenderer(adoptPtr(new LayerRendererChromium(owner, context)));
139     if (!layerRenderer->initialize())
140         return nullptr;
141
142     return layerRenderer.release();
143 }
144
145 LayerRendererChromium::LayerRendererChromium(CCLayerTreeHostImpl* owner,
146                                              PassRefPtr<GraphicsContext3D> context)
147     : m_owner(owner)
148     , m_currentRenderSurface(0)
149     , m_offscreenFramebufferId(0)
150     , m_zoomAnimatorScale(1)
151     , m_context(context)
152     , m_defaultRenderSurface(0)
153     , m_sharedGeometryQuad(FloatRect(-0.5f, -0.5f, 1.0f, 1.0f))
154 {
155 }
156
157 bool LayerRendererChromium::initialize()
158 {
159     m_context->makeContextCurrent();
160     if (settings().acceleratePainting) {
161         m_capabilities.usingAcceleratedPainting = true;
162     }
163
164     WebCore::Extensions3D* extensions = m_context->getExtensions();
165     m_capabilities.usingMapSub = extensions->supports("GL_CHROMIUM_map_sub");
166     if (m_capabilities.usingMapSub)
167         extensions->ensureEnabled("GL_CHROMIUM_map_sub");
168
169     GLC(m_context.get(), m_context->getIntegerv(GraphicsContext3D::MAX_TEXTURE_SIZE, &m_capabilities.maxTextureSize));
170     m_capabilities.bestTextureFormat = PlatformColor::bestTextureFormat(m_context.get());
171
172     if (!initializeSharedObjects())
173         return false;
174
175     m_headsUpDisplay = CCHeadsUpDisplay::create(this);
176
177     // Make sure the viewport and context gets initialized, even if it is to zero.
178     viewportChanged();
179     return true;
180 }
181
182 LayerRendererChromium::~LayerRendererChromium()
183 {
184     m_headsUpDisplay.clear(); // Explicitly destroy the HUD before the TextureManager dies.
185     cleanupSharedObjects();
186 }
187
188 void LayerRendererChromium::clearRenderSurfacesOnCCLayerImplRecursive(CCLayerImpl* layer)
189 {
190     for (size_t i = 0; i < layer->children().size(); ++i)
191         clearRenderSurfacesOnCCLayerImplRecursive(layer->children()[i].get());
192     layer->clearRenderSurface();
193 }
194
195 void LayerRendererChromium::close()
196 {
197     if (rootLayer())
198         clearRenderSurfacesOnCCLayerImplRecursive(rootLayer());
199 }
200
201 GraphicsContext3D* LayerRendererChromium::context()
202 {
203     return m_context.get();
204 }
205
206 void LayerRendererChromium::debugGLCall(GraphicsContext3D* context, const char* command, const char* file, int line)
207 {
208     unsigned long error = context->getError();
209     if (error != GraphicsContext3D::NO_ERROR)
210         LOG_ERROR("GL command failed: File: %s\n\tLine %d\n\tcommand: %s, error %x\n", file, line, command, static_cast<int>(error));
211 }
212
213 void LayerRendererChromium::releaseRenderSurfaceTextures()
214 {
215     m_renderSurfaceTextureManager->evictAndDeleteAllTextures(m_context.get());
216 }
217
218 void LayerRendererChromium::viewportChanged()
219 {
220     if (m_context)
221         m_context->reshape(std::max(1, viewportWidth()), std::max(1, viewportHeight()));
222
223     // Reset the current render surface to force an update of the viewport and
224     // projection matrix next time useRenderSurface is called.
225     m_currentRenderSurface = 0;
226 }
227
228 void LayerRendererChromium::drawLayers()
229 {
230     // FIXME: use the frame begin time from the overall compositor scheduler.
231     // This value is currently inaccessible because it is up in Chromium's
232     // RenderWidget.
233     m_headsUpDisplay->onFrameBegin(currentTime());
234
235     if (!rootLayer())
236         return;
237
238     m_renderSurfaceTextureManager->setMemoryLimitBytes(TextureManager::highLimitBytes() - m_contentsTextureMemoryUseBytes);
239     drawLayersInternal();
240
241     if (TextureManager::reclaimLimitBytes() > m_contentsTextureMemoryUseBytes)
242         m_renderSurfaceTextureManager->reduceMemoryToLimit(TextureManager::reclaimLimitBytes() - m_contentsTextureMemoryUseBytes);
243     else
244         m_renderSurfaceTextureManager->reduceMemoryToLimit(0);
245     m_renderSurfaceTextureManager->deleteEvictedTextures(m_context.get());
246
247     if (settings().compositeOffscreen)
248         copyOffscreenTextureToDisplay();
249 }
250
251 void LayerRendererChromium::drawLayersInternal()
252 {
253     if (viewportSize().isEmpty() || !rootLayer())
254         return;
255
256     TRACE_EVENT("LayerRendererChromium::drawLayers", this, 0);
257     CCLayerImpl* rootDrawLayer = rootLayer();
258     makeContextCurrent();
259
260     if (!rootDrawLayer->renderSurface())
261         rootDrawLayer->createRenderSurface();
262     rootDrawLayer->renderSurface()->setContentRect(IntRect(IntPoint(), viewportSize()));
263
264     rootDrawLayer->setScissorRect(IntRect(IntPoint(), viewportSize()));
265
266     CCLayerList renderSurfaceLayerList;
267     renderSurfaceLayerList.append(rootDrawLayer);
268
269     TransformationMatrix zoomMatrix;
270     zoomMatrix.scale3d(m_zoomAnimatorScale, m_zoomAnimatorScale, 1);
271     m_defaultRenderSurface = rootDrawLayer->renderSurface();
272     m_defaultRenderSurface->clearLayerList();
273
274     {
275         TRACE_EVENT("LayerRendererChromium::drawLayersInternal::calcDrawEtc", this, 0);
276         CCLayerTreeHostCommon::calculateDrawTransformsAndVisibility(rootDrawLayer, rootDrawLayer, zoomMatrix, zoomMatrix, renderSurfaceLayerList, m_defaultRenderSurface->layerList(), &m_layerSorter, m_capabilities.maxTextureSize);
277     }
278
279     // The GL viewport covers the entire visible area, including the scrollbars.
280     GLC(m_context.get(), m_context->viewport(0, 0, viewportWidth(), viewportHeight()));
281     m_windowMatrix = screenMatrix(0, 0, viewportWidth(), viewportHeight());
282
283     // Bind the common vertex attributes used for drawing all the layers.
284     m_sharedGeometry->prepareForDraw();
285
286     GLC(m_context.get(), m_context->disable(GraphicsContext3D::SCISSOR_TEST));
287     GLC(m_context.get(), m_context->disable(GraphicsContext3D::DEPTH_TEST));
288     GLC(m_context.get(), m_context->disable(GraphicsContext3D::CULL_FACE));
289
290     useRenderSurface(m_defaultRenderSurface);
291
292     // Clear to blue to make it easier to spot unrendered regions.
293     m_context->clearColor(0, 0, 1, 1);
294     m_context->colorMask(true, true, true, true);
295     m_context->clear(GraphicsContext3D::COLOR_BUFFER_BIT);
296
297     GLC(m_context.get(), m_context->enable(GraphicsContext3D::BLEND));
298     GLC(m_context.get(), m_context->blendFunc(GraphicsContext3D::ONE, GraphicsContext3D::ONE_MINUS_SRC_ALPHA));
299     GLC(m_context.get(), m_context->enable(GraphicsContext3D::SCISSOR_TEST));
300
301     // Update the contents of the render surfaces. We traverse the array from
302     // back to front to guarantee that nested render surfaces get rendered in the
303     // correct order.
304     for (int surfaceIndex = renderSurfaceLayerList.size() - 1; surfaceIndex >= 0 ; --surfaceIndex) {
305         CCLayerImpl* renderSurfaceLayer = renderSurfaceLayerList[surfaceIndex].get();
306         CCRenderSurface* renderSurface = renderSurfaceLayer->renderSurface();
307         ASSERT(renderSurface);
308
309         renderSurface->setSkipsDraw(true);
310
311         // Render surfaces whose drawable area has zero width or height
312         // will have no layers associated with them and should be skipped.
313         if (!renderSurface->layerList().size())
314             continue;
315
316         // Skip completely transparent render surfaces.
317         if (!renderSurface->drawOpacity())
318             continue;
319
320         if (useRenderSurface(renderSurface)) {
321             renderSurface->setSkipsDraw(false);
322
323             if (renderSurfaceLayer != rootDrawLayer) {
324                 GLC(m_context.get(), m_context->disable(GraphicsContext3D::SCISSOR_TEST));
325                 GLC(m_context.get(), m_context->clearColor(0, 0, 0, 0));
326                 GLC(m_context.get(), m_context->clear(GraphicsContext3D::COLOR_BUFFER_BIT));
327                 GLC(m_context.get(), m_context->enable(GraphicsContext3D::SCISSOR_TEST));
328             }
329
330             const CCLayerList& layerList = renderSurface->layerList();
331             for (unsigned layerIndex = 0; layerIndex < layerList.size(); ++layerIndex)
332                 drawLayer(layerList[layerIndex].get(), renderSurface);
333         }
334     }
335
336     if (m_headsUpDisplay->enabled()) {
337         GLC(m_context.get(), m_context->enable(GraphicsContext3D::BLEND));
338         GLC(m_context.get(), m_context->blendFunc(GraphicsContext3D::ONE, GraphicsContext3D::ONE_MINUS_SRC_ALPHA));
339         GLC(m_context.get(), m_context->disable(GraphicsContext3D::SCISSOR_TEST));
340         useRenderSurface(m_defaultRenderSurface);
341         m_headsUpDisplay->draw();
342     }
343
344     GLC(m_context.get(), m_context->disable(GraphicsContext3D::SCISSOR_TEST));
345     GLC(m_context.get(), m_context->disable(GraphicsContext3D::BLEND));
346 }
347
348 void LayerRendererChromium::finish()
349 {
350     TRACE_EVENT("LayerRendererChromium::finish", this, 0);
351     m_context->finish();
352 }
353
354 void LayerRendererChromium::present()
355 {
356     TRACE_EVENT("LayerRendererChromium::present", this, 0);
357     // We're done! Time to swapbuffers!
358
359     // Note that currently this has the same effect as swapBuffers; we should
360     // consider exposing a different entry point on GraphicsContext3D.
361     m_context->prepareTexture();
362
363     m_headsUpDisplay->onPresent();
364 }
365
366 void LayerRendererChromium::getFramebufferPixels(void *pixels, const IntRect& rect)
367 {
368     ASSERT(rect.maxX() <= viewportWidth() && rect.maxY() <= viewportHeight());
369
370     if (!pixels)
371         return;
372
373     makeContextCurrent();
374
375     GLC(m_context.get(), m_context->readPixels(rect.x(), rect.y(), rect.width(), rect.height(),
376                                          GraphicsContext3D::RGBA, GraphicsContext3D::UNSIGNED_BYTE, pixels));
377 }
378
379 // FIXME: This method should eventually be replaced by a proper texture manager.
380 unsigned LayerRendererChromium::createLayerTexture()
381 {
382     unsigned textureId = 0;
383     GLC(m_context.get(), textureId = m_context->createTexture());
384     GLC(m_context.get(), m_context->bindTexture(GraphicsContext3D::TEXTURE_2D, textureId));
385     // Do basic linear filtering on resize.
386     GLC(m_context.get(), m_context->texParameteri(GraphicsContext3D::TEXTURE_2D, GraphicsContext3D::TEXTURE_MIN_FILTER, GraphicsContext3D::LINEAR));
387     GLC(m_context.get(), m_context->texParameteri(GraphicsContext3D::TEXTURE_2D, GraphicsContext3D::TEXTURE_MAG_FILTER, GraphicsContext3D::LINEAR));
388     // NPOT textures in GL ES only work when the wrap mode is set to GraphicsContext3D::CLAMP_TO_EDGE.
389     GLC(m_context.get(), m_context->texParameteri(GraphicsContext3D::TEXTURE_2D, GraphicsContext3D::TEXTURE_WRAP_S, GraphicsContext3D::CLAMP_TO_EDGE));
390     GLC(m_context.get(), m_context->texParameteri(GraphicsContext3D::TEXTURE_2D, GraphicsContext3D::TEXTURE_WRAP_T, GraphicsContext3D::CLAMP_TO_EDGE));
391     return textureId;
392 }
393
394 void LayerRendererChromium::deleteLayerTexture(unsigned textureId)
395 {
396     if (!textureId)
397         return;
398
399     GLC(m_context.get(), m_context->deleteTexture(textureId));
400 }
401
402 // Returns true if any part of the layer falls within the visibleRect
403 bool LayerRendererChromium::isLayerVisible(LayerChromium* layer, const TransformationMatrix& matrix, const IntRect& visibleRect)
404 {
405     // Form the matrix used by the shader to map the corners of the layer's
406     // bounds into clip space.
407     TransformationMatrix renderMatrix = matrix;
408     renderMatrix.scale3d(layer->bounds().width(), layer->bounds().height(), 1);
409     renderMatrix = m_projectionMatrix * renderMatrix;
410
411     FloatRect layerRect(-0.5, -0.5, 1, 1);
412     FloatRect mappedRect = renderMatrix.mapRect(layerRect);
413
414     // The layer is visible if it intersects any part of a rectangle whose origin
415     // is at (-1, -1) and size is 2x2.
416     return mappedRect.intersects(FloatRect(-1, -1, 2, 2));
417 }
418
419 ManagedTexture* LayerRendererChromium::getOffscreenLayerTexture()
420 {
421     return settings().compositeOffscreen && rootLayer() ? rootLayer()->renderSurface()->contentsTexture() : 0;
422 }
423
424 void LayerRendererChromium::copyOffscreenTextureToDisplay()
425 {
426     if (settings().compositeOffscreen) {
427         makeContextCurrent();
428
429         useRenderSurface(0);
430         TransformationMatrix drawTransform;
431         drawTransform.translate3d(0.5 * m_defaultRenderSurface->contentRect().width(), 0.5 * m_defaultRenderSurface->contentRect().height(), 0);
432         m_defaultRenderSurface->setDrawTransform(drawTransform);
433         m_defaultRenderSurface->setDrawOpacity(1);
434         m_defaultRenderSurface->draw(this, m_defaultRenderSurface->contentRect());
435     }
436 }
437
438 bool LayerRendererChromium::useRenderSurface(CCRenderSurface* renderSurface)
439 {
440     if (m_currentRenderSurface == renderSurface)
441         return true;
442
443     m_currentRenderSurface = renderSurface;
444
445     if ((renderSurface == m_defaultRenderSurface && !settings().compositeOffscreen) || (!renderSurface && settings().compositeOffscreen)) {
446         GLC(m_context.get(), m_context->bindFramebuffer(GraphicsContext3D::FRAMEBUFFER, 0));
447         if (renderSurface)
448             setDrawViewportRect(renderSurface->contentRect(), true);
449         else
450             setDrawViewportRect(m_defaultRenderSurface->contentRect(), true);
451         return true;
452     }
453
454     GLC(m_context.get(), m_context->bindFramebuffer(GraphicsContext3D::FRAMEBUFFER, m_offscreenFramebufferId));
455
456     if (!renderSurface->prepareContentsTexture(this))
457         return false;
458
459     renderSurface->contentsTexture()->framebufferTexture2D(m_context.get());
460
461 #if !defined ( NDEBUG )
462     if (m_context->checkFramebufferStatus(GraphicsContext3D::FRAMEBUFFER) != GraphicsContext3D::FRAMEBUFFER_COMPLETE) {
463         ASSERT_NOT_REACHED();
464         return false;
465     }
466 #endif
467
468     setDrawViewportRect(renderSurface->contentRect(), false);
469     return true;
470 }
471
472 void LayerRendererChromium::drawLayer(CCLayerImpl* layer, CCRenderSurface* targetSurface)
473 {
474     if (layer->renderSurface() && layer->renderSurface() != targetSurface) {
475         layer->renderSurface()->draw(this, layer->getDrawRect());
476         layer->renderSurface()->releaseContentsTexture();
477         return;
478     }
479
480     if (!layer->drawsContent())
481         return;
482
483     if (!layer->opacity())
484         return;
485
486     if (layer->bounds().isEmpty())
487         return;
488
489     IntRect targetSurfaceRect = layer->targetRenderSurface() ? layer->targetRenderSurface()->contentRect() : m_defaultRenderSurface->contentRect();
490     if (layer->usesLayerScissor()) {
491         IntRect scissorRect = layer->scissorRect();
492         targetSurfaceRect.intersect(scissorRect);
493         if (targetSurfaceRect.isEmpty())
494             return;
495         setScissorToRect(scissorRect);
496     } else
497         GLC(m_context.get(), m_context->disable(GraphicsContext3D::SCISSOR_TEST));
498
499     // The layer should not be drawn if (1) it is not double-sided and (2) the back of the layer is facing the screen.
500     // This second condition is checked by computing the transformed normal of the layer.
501     if (!layer->doubleSided()) {
502         FloatRect layerRect(FloatPoint(0, 0), FloatSize(layer->bounds()));
503         FloatQuad mappedLayer = layer->screenSpaceTransform().mapQuad(FloatQuad(layerRect));
504         FloatSize horizontalDir = mappedLayer.p2() - mappedLayer.p1();
505         FloatSize verticalDir = mappedLayer.p4() - mappedLayer.p1();
506         FloatPoint3D xAxis(horizontalDir.width(), horizontalDir.height(), 0);
507         FloatPoint3D yAxis(verticalDir.width(), verticalDir.height(), 0);
508         FloatPoint3D zAxis = xAxis.cross(yAxis);
509         if (zAxis.z() < 0)
510             return;
511     }
512
513     layer->draw(this);
514
515     // Draw the debug border if there is one.
516     layer->drawDebugBorder(this);
517 }
518
519 // Sets the scissor region to the given rectangle. The coordinate system for the
520 // scissorRect has its origin at the top left corner of the current visible rect.
521 void LayerRendererChromium::setScissorToRect(const IntRect& scissorRect)
522 {
523     IntRect contentRect = (m_currentRenderSurface ? m_currentRenderSurface->contentRect() : m_defaultRenderSurface->contentRect());
524
525     GLC(m_context.get(), m_context->enable(GraphicsContext3D::SCISSOR_TEST));
526
527     // The scissor coordinates must be supplied in viewport space so we need to offset
528     // by the relative position of the top left corner of the current render surface.
529     int scissorX = scissorRect.x() - contentRect.x();
530     // When rendering to the default render surface we're rendering upside down so the top
531     // of the GL scissor is the bottom of our layer.
532     // But, if rendering to offscreen texture, we reverse our sense of 'upside down'.
533     int scissorY;
534     if (m_currentRenderSurface == m_defaultRenderSurface && !settings().compositeOffscreen)
535         scissorY = m_currentRenderSurface->contentRect().height() - (scissorRect.maxY() - m_currentRenderSurface->contentRect().y());
536     else
537         scissorY = scissorRect.y() - contentRect.y();
538     GLC(m_context.get(), m_context->scissor(scissorX, scissorY, scissorRect.width(), scissorRect.height()));
539 }
540
541 bool LayerRendererChromium::makeContextCurrent()
542 {
543     m_context->makeContextCurrent();
544     return true;
545 }
546
547 // Sets the coordinate range of content that ends being drawn onto the target render surface.
548 // The target render surface is assumed to have an origin at 0, 0 and the width and height of
549 // of the drawRect.
550 void LayerRendererChromium::setDrawViewportRect(const IntRect& drawRect, bool flipY)
551 {
552     if (flipY)
553         m_projectionMatrix = orthoMatrix(drawRect.x(), drawRect.maxX(), drawRect.maxY(), drawRect.y());
554     else
555         m_projectionMatrix = orthoMatrix(drawRect.x(), drawRect.maxX(), drawRect.y(), drawRect.maxY());
556     GLC(m_context.get(), m_context->viewport(0, 0, drawRect.width(), drawRect.height()));
557     m_windowMatrix = screenMatrix(0, 0, drawRect.width(), drawRect.height());
558 }
559
560
561 bool LayerRendererChromium::initializeSharedObjects()
562 {
563     TRACE_EVENT("LayerRendererChromium::initializeSharedObjects", this, 0);
564     makeContextCurrent();
565
566     // Create an FBO for doing offscreen rendering.
567     GLC(m_context.get(), m_offscreenFramebufferId = m_context->createFramebuffer());
568
569     // We will always need these programs to render, so create the programs eagerly so that the shader compilation can
570     // start while we do other work. Other programs are created lazily on first access.
571     m_sharedGeometry = adoptPtr(new GeometryBinding(m_context.get()));
572     m_renderSurfaceProgram = adoptPtr(new CCRenderSurface::Program(m_context.get()));
573     m_tilerProgram = adoptPtr(new CCTiledLayerImpl::Program(m_context.get()));
574
575     GLC(m_context.get(), m_context->flush());
576
577     m_renderSurfaceTextureManager = TextureManager::create(TextureManager::highLimitBytes(), m_capabilities.maxTextureSize);
578     return true;
579 }
580
581 const LayerChromium::BorderProgram* LayerRendererChromium::borderProgram()
582 {
583     if (!m_borderProgram)
584         m_borderProgram = adoptPtr(new LayerChromium::BorderProgram(m_context.get()));
585     if (!m_borderProgram->initialized()) {
586         TRACE_EVENT("LayerRendererChromium::borderProgram::initialize", this, 0);
587         m_borderProgram->initialize(m_context.get());
588     }
589     return m_borderProgram.get();
590 }
591
592 const CCHeadsUpDisplay::Program* LayerRendererChromium::headsUpDisplayProgram()
593 {
594     if (!m_headsUpDisplayProgram)
595         m_headsUpDisplayProgram = adoptPtr(new CCHeadsUpDisplay::Program(m_context.get()));
596     if (!m_headsUpDisplayProgram->initialized()) {
597         TRACE_EVENT("LayerRendererChromium::headsUpDisplayProgram::initialize", this, 0);
598         m_headsUpDisplayProgram->initialize(m_context.get());
599     }
600     return m_headsUpDisplayProgram.get();
601 }
602
603 const CCRenderSurface::Program* LayerRendererChromium::renderSurfaceProgram()
604 {
605     ASSERT(m_renderSurfaceProgram);
606     if (!m_renderSurfaceProgram->initialized()) {
607         TRACE_EVENT("LayerRendererChromium::renderSurfaceProgram::initialize", this, 0);
608         m_renderSurfaceProgram->initialize(m_context.get());
609     }
610     return m_renderSurfaceProgram.get();
611 }
612
613 const CCRenderSurface::ProgramAA* LayerRendererChromium::renderSurfaceProgramAA()
614 {
615     if (!m_renderSurfaceProgramAA)
616         m_renderSurfaceProgramAA = adoptPtr(new CCRenderSurface::ProgramAA(m_context.get()));
617     if (!m_renderSurfaceProgramAA->initialized()) {
618         TRACE_EVENT("LayerRendererChromium::renderSurfaceProgramAA::initialize", this, 0);
619         m_renderSurfaceProgramAA->initialize(m_context.get());
620     }
621     return m_renderSurfaceProgramAA.get();
622 }
623
624 const CCRenderSurface::MaskProgram* LayerRendererChromium::renderSurfaceMaskProgram()
625 {
626     if (!m_renderSurfaceMaskProgram)
627         m_renderSurfaceMaskProgram = adoptPtr(new CCRenderSurface::MaskProgram(m_context.get()));
628     if (!m_renderSurfaceMaskProgram->initialized()) {
629         TRACE_EVENT("LayerRendererChromium::renderSurfaceMaskProgram::initialize", this, 0);
630         m_renderSurfaceMaskProgram->initialize(m_context.get());
631     }
632     return m_renderSurfaceMaskProgram.get();
633 }
634
635 const CCRenderSurface::MaskProgramAA* LayerRendererChromium::renderSurfaceMaskProgramAA()
636 {
637     if (!m_renderSurfaceMaskProgramAA)
638         m_renderSurfaceMaskProgramAA = adoptPtr(new CCRenderSurface::MaskProgramAA(m_context.get()));
639     if (!m_renderSurfaceMaskProgramAA->initialized()) {
640         TRACE_EVENT("LayerRendererChromium::renderSurfaceMaskProgramAA::initialize", this, 0);
641         m_renderSurfaceMaskProgramAA->initialize(m_context.get());
642     }
643     return m_renderSurfaceMaskProgramAA.get();
644 }
645
646 const CCTiledLayerImpl::Program* LayerRendererChromium::tilerProgram()
647 {
648     ASSERT(m_tilerProgram);
649     if (!m_tilerProgram->initialized()) {
650         TRACE_EVENT("LayerRendererChromium::tilerProgram::initialize", this, 0);
651         m_tilerProgram->initialize(m_context.get());
652     }
653     return m_tilerProgram.get();
654 }
655
656 const CCTiledLayerImpl::ProgramAA* LayerRendererChromium::tilerProgramAA()
657 {
658     if (!m_tilerProgramAA)
659         m_tilerProgramAA = adoptPtr(new CCTiledLayerImpl::ProgramAA(m_context.get()));
660     if (!m_tilerProgramAA->initialized()) {
661         TRACE_EVENT("LayerRendererChromium::tilerProgramAA::initialize", this, 0);
662         m_tilerProgramAA->initialize(m_context.get());
663     }
664     return m_tilerProgramAA.get();
665 }
666
667 const CCTiledLayerImpl::ProgramSwizzle* LayerRendererChromium::tilerProgramSwizzle()
668 {
669     if (!m_tilerProgramSwizzle)
670         m_tilerProgramSwizzle = adoptPtr(new CCTiledLayerImpl::ProgramSwizzle(m_context.get()));
671     if (!m_tilerProgramSwizzle->initialized()) {
672         TRACE_EVENT("LayerRendererChromium::tilerProgramSwizzle::initialize", this, 0);
673         m_tilerProgramSwizzle->initialize(m_context.get());
674     }
675     return m_tilerProgramSwizzle.get();
676 }
677
678 const CCTiledLayerImpl::ProgramSwizzleAA* LayerRendererChromium::tilerProgramSwizzleAA()
679 {
680     if (!m_tilerProgramSwizzleAA)
681         m_tilerProgramSwizzleAA = adoptPtr(new CCTiledLayerImpl::ProgramSwizzleAA(m_context.get()));
682     if (!m_tilerProgramSwizzleAA->initialized()) {
683         TRACE_EVENT("LayerRendererChromium::tilerProgramSwizzleAA::initialize", this, 0);
684         m_tilerProgramSwizzleAA->initialize(m_context.get());
685     }
686     return m_tilerProgramSwizzleAA.get();
687 }
688
689 const CCCanvasLayerImpl::Program* LayerRendererChromium::canvasLayerProgram()
690 {
691     if (!m_canvasLayerProgram)
692         m_canvasLayerProgram = adoptPtr(new CCCanvasLayerImpl::Program(m_context.get()));
693     if (!m_canvasLayerProgram->initialized()) {
694         TRACE_EVENT("LayerRendererChromium::canvasLayerProgram::initialize", this, 0);
695         m_canvasLayerProgram->initialize(m_context.get());
696     }
697     return m_canvasLayerProgram.get();
698 }
699
700 const CCPluginLayerImpl::Program* LayerRendererChromium::pluginLayerProgram()
701 {
702     if (!m_pluginLayerProgram)
703         m_pluginLayerProgram = adoptPtr(new CCPluginLayerImpl::Program(m_context.get()));
704     if (!m_pluginLayerProgram->initialized()) {
705         TRACE_EVENT("LayerRendererChromium::pluginLayerProgram::initialize", this, 0);
706         m_pluginLayerProgram->initialize(m_context.get());
707     }
708     return m_pluginLayerProgram.get();
709 }
710
711 const CCVideoLayerImpl::RGBAProgram* LayerRendererChromium::videoLayerRGBAProgram()
712 {
713     if (!m_videoLayerRGBAProgram)
714         m_videoLayerRGBAProgram = adoptPtr(new CCVideoLayerImpl::RGBAProgram(m_context.get()));
715     if (!m_videoLayerRGBAProgram->initialized()) {
716         TRACE_EVENT("LayerRendererChromium::videoLayerRGBAProgram::initialize", this, 0);
717         m_videoLayerRGBAProgram->initialize(m_context.get());
718     }
719     return m_videoLayerRGBAProgram.get();
720 }
721
722 const CCVideoLayerImpl::YUVProgram* LayerRendererChromium::videoLayerYUVProgram()
723 {
724     if (!m_videoLayerYUVProgram)
725         m_videoLayerYUVProgram = adoptPtr(new CCVideoLayerImpl::YUVProgram(m_context.get()));
726     if (!m_videoLayerYUVProgram->initialized()) {
727         TRACE_EVENT("LayerRendererChromium::videoLayerYUVProgram::initialize", this, 0);
728         m_videoLayerYUVProgram->initialize(m_context.get());
729     }
730     return m_videoLayerYUVProgram.get();
731 }
732
733
734 void LayerRendererChromium::cleanupSharedObjects()
735 {
736     makeContextCurrent();
737
738     m_sharedGeometry.clear();
739
740     if (m_borderProgram)
741         m_borderProgram->cleanup(m_context.get());
742     if (m_headsUpDisplayProgram)
743         m_headsUpDisplayProgram->cleanup(m_context.get());
744     if (m_tilerProgram)
745         m_tilerProgram->cleanup(m_context.get());
746     if (m_tilerProgramAA)
747         m_tilerProgramAA->cleanup(m_context.get());
748     if (m_tilerProgramSwizzle)
749         m_tilerProgramSwizzle->cleanup(m_context.get());
750     if (m_tilerProgramSwizzleAA)
751         m_tilerProgramSwizzleAA->cleanup(m_context.get());
752     if (m_canvasLayerProgram)
753         m_canvasLayerProgram->cleanup(m_context.get());
754     if (m_pluginLayerProgram)
755         m_pluginLayerProgram->cleanup(m_context.get());
756     if (m_renderSurfaceMaskProgram)
757         m_renderSurfaceMaskProgram->cleanup(m_context.get());
758     if (m_renderSurfaceMaskProgramAA)
759         m_renderSurfaceMaskProgramAA->cleanup(m_context.get());
760     if (m_renderSurfaceProgram)
761         m_renderSurfaceProgram->cleanup(m_context.get());
762     if (m_renderSurfaceProgramAA)
763         m_renderSurfaceProgramAA->cleanup(m_context.get());
764     if (m_videoLayerRGBAProgram)
765         m_videoLayerRGBAProgram->cleanup(m_context.get());
766     if (m_videoLayerYUVProgram)
767         m_videoLayerYUVProgram->cleanup(m_context.get());
768
769     m_borderProgram.clear();
770     m_headsUpDisplayProgram.clear();
771     m_tilerProgram.clear();
772     m_tilerProgramAA.clear();
773     m_tilerProgramSwizzle.clear();
774     m_tilerProgramSwizzleAA.clear();
775     m_canvasLayerProgram.clear();
776     m_pluginLayerProgram.clear();
777     m_renderSurfaceMaskProgram.clear();
778     m_renderSurfaceMaskProgramAA.clear();
779     m_renderSurfaceProgram.clear();
780     m_renderSurfaceProgramAA.clear();
781     m_videoLayerRGBAProgram.clear();
782     m_videoLayerYUVProgram.clear();
783     if (m_offscreenFramebufferId)
784         GLC(m_context.get(), m_context->deleteFramebuffer(m_offscreenFramebufferId));
785
786     ASSERT(!m_contentsTextureMemoryUseBytes);
787     releaseRenderSurfaceTextures();
788 }
789
790 String LayerRendererChromium::layerTreeAsText() const
791 {
792     TextStream ts;
793     if (rootLayer()) {
794         ts << rootLayer()->layerTreeAsText();
795         ts << "RenderSurfaces:\n";
796         dumpRenderSurfaces(ts, 1, rootLayer());
797     }
798     return ts.release();
799 }
800
801 void LayerRendererChromium::dumpRenderSurfaces(TextStream& ts, int indent, const CCLayerImpl* layer) const
802 {
803     if (layer->renderSurface())
804         layer->renderSurface()->dumpSurface(ts, indent);
805
806     for (size_t i = 0; i < layer->children().size(); ++i)
807         dumpRenderSurfaces(ts, indent, layer->children()[i].get());
808 }
809
810 bool LayerRendererChromium::isContextLost()
811 {
812     return (m_context.get()->getExtensions()->getGraphicsResetStatusARB() != GraphicsContext3D::NO_ERROR);
813 }
814
815 } // namespace WebCore
816
817 #endif // USE(ACCELERATED_COMPOSITING)