WebKit Roll 139512:139548
[chromium-blink-merge.git] / cc / io_surface_layer_impl.cc
blob9ea69e2b6bdd03c6a4eebcc0b6517f24b7f80d13
1 // Copyright 2012 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 #include "cc/io_surface_layer_impl.h"
7 #include "base/stringprintf.h"
8 #include "cc/gl_renderer.h" // For the GLC() macro.
9 #include "cc/io_surface_draw_quad.h"
10 #include "cc/layer_tree_impl.h"
11 #include "cc/output_surface.h"
12 #include "cc/quad_sink.h"
13 #include "gpu/GLES2/gl2extchromium.h"
14 #include "third_party/WebKit/Source/Platform/chromium/public/WebGraphicsContext3D.h"
15 #include "third_party/khronos/GLES2/gl2.h"
16 #include "third_party/khronos/GLES2/gl2ext.h"
18 namespace cc {
20 IOSurfaceLayerImpl::IOSurfaceLayerImpl(LayerTreeImpl* treeImpl, int id)
21 : LayerImpl(treeImpl, id)
22 , m_ioSurfaceId(0)
23 , m_ioSurfaceChanged(false)
24 , m_ioSurfaceTextureId(0)
28 IOSurfaceLayerImpl::~IOSurfaceLayerImpl()
30 if (!m_ioSurfaceTextureId)
31 return;
33 OutputSurface* outputSurface = layerTreeImpl()->output_surface();
34 // FIXME: Implement this path for software compositing.
35 WebKit::WebGraphicsContext3D* context3d = outputSurface->Context3D();
36 if (context3d)
37 context3d->deleteTexture(m_ioSurfaceTextureId);
40 void IOSurfaceLayerImpl::willDraw(ResourceProvider* resourceProvider)
42 LayerImpl::willDraw(resourceProvider);
44 if (m_ioSurfaceChanged) {
45 WebKit::WebGraphicsContext3D* context3d = resourceProvider->graphicsContext3D();
46 if (!context3d) {
47 // FIXME: Implement this path for software compositing.
48 return;
51 // FIXME: Do this in a way that we can track memory usage.
52 if (!m_ioSurfaceTextureId)
53 m_ioSurfaceTextureId = context3d->createTexture();
55 GLC(context3d, context3d->activeTexture(GL_TEXTURE0));
56 GLC(context3d, context3d->bindTexture(GL_TEXTURE_RECTANGLE_ARB, m_ioSurfaceTextureId));
57 GLC(context3d, context3d->texParameteri(GL_TEXTURE_RECTANGLE_ARB, GL_TEXTURE_MIN_FILTER, GL_LINEAR));
58 GLC(context3d, context3d->texParameteri(GL_TEXTURE_RECTANGLE_ARB, GL_TEXTURE_MAG_FILTER, GL_LINEAR));
59 GLC(context3d, context3d->texParameteri(GL_TEXTURE_RECTANGLE_ARB, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE));
60 GLC(context3d, context3d->texParameteri(GL_TEXTURE_RECTANGLE_ARB, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE));
61 context3d->texImageIOSurface2DCHROMIUM(GL_TEXTURE_RECTANGLE_ARB,
62 m_ioSurfaceSize.width(),
63 m_ioSurfaceSize.height(),
64 m_ioSurfaceId,
65 0);
66 // Do not check for error conditions. texImageIOSurface2DCHROMIUM is supposed to hold on to
67 // the last good IOSurface if the new one is already closed. This is only a possibility
68 // during live resizing of plugins. However, it seems that this is not sufficient to
69 // completely guard against garbage being drawn. If this is found to be a significant issue,
70 // it may be necessary to explicitly tell the embedder when to free the surfaces it has
71 // allocated.
72 m_ioSurfaceChanged = false;
76 void IOSurfaceLayerImpl::appendQuads(QuadSink& quadSink, AppendQuadsData& appendQuadsData)
78 SharedQuadState* sharedQuadState = quadSink.useSharedQuadState(createSharedQuadState());
79 appendDebugBorderQuad(quadSink, sharedQuadState, appendQuadsData);
81 gfx::Rect quadRect(gfx::Point(), contentBounds());
82 gfx::Rect opaqueRect(contentsOpaque() ? quadRect : gfx::Rect());
83 scoped_ptr<IOSurfaceDrawQuad> quad = IOSurfaceDrawQuad::Create();
84 quad->SetNew(sharedQuadState, quadRect, opaqueRect, m_ioSurfaceSize, m_ioSurfaceTextureId, IOSurfaceDrawQuad::FLIPPED);
85 quadSink.append(quad.PassAs<DrawQuad>(), appendQuadsData);
88 void IOSurfaceLayerImpl::dumpLayerProperties(std::string* str, int indent) const
90 str->append(indentString(indent));
91 base::StringAppendF(str, "iosurface id: %u texture id: %u\n", m_ioSurfaceId, m_ioSurfaceTextureId);
92 LayerImpl::dumpLayerProperties(str, indent);
95 void IOSurfaceLayerImpl::didLoseOutputSurface()
97 // We don't have a valid texture ID in the new context; however,
98 // the IOSurface is still valid.
99 m_ioSurfaceTextureId = 0;
100 m_ioSurfaceChanged = true;
103 void IOSurfaceLayerImpl::setIOSurfaceProperties(unsigned ioSurfaceId, const gfx::Size& size)
105 if (m_ioSurfaceId != ioSurfaceId)
106 m_ioSurfaceChanged = true;
108 m_ioSurfaceId = ioSurfaceId;
109 m_ioSurfaceSize = size;
112 const char* IOSurfaceLayerImpl::layerTypeAsString() const
114 return "IOSurfaceLayer";
117 } // namespace cc