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.
7 #include "CCIOSurfaceLayerImpl.h"
9 #include "base/stringprintf.h"
10 #include "CCGraphicsContext.h"
11 #include "CCIOSurfaceDrawQuad.h"
12 #include "CCLayerTreeHostImpl.h"
13 #include "CCQuadSink.h"
14 #include "CCRendererGL.h" // For the GLC() macro.
15 #include "Extensions3D.h"
16 #include <public/WebGraphicsContext3D.h>
20 CCIOSurfaceLayerImpl::CCIOSurfaceLayerImpl(int id
)
23 , m_ioSurfaceChanged(false)
24 , m_ioSurfaceTextureId(0)
28 CCIOSurfaceLayerImpl::~CCIOSurfaceLayerImpl()
30 if (!m_ioSurfaceTextureId
)
33 CCGraphicsContext
* context
= layerTreeHostImpl()->context();
34 // FIXME: Implement this path for software compositing.
35 WebKit::WebGraphicsContext3D
* context3d
= context
->context3D();
37 context3d
->deleteTexture(m_ioSurfaceTextureId
);
40 void CCIOSurfaceLayerImpl::willDraw(CCResourceProvider
* resourceProvider
)
42 CCLayerImpl::willDraw(resourceProvider
);
44 if (m_ioSurfaceChanged
) {
45 WebKit::WebGraphicsContext3D
* context3d
= resourceProvider
->graphicsContext3D();
47 // FIXME: Implement this path for software compositing.
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(GraphicsContext3D::TEXTURE0
));
56 GLC(context3d
, context3d
->bindTexture(Extensions3D::TEXTURE_RECTANGLE_ARB
, m_ioSurfaceTextureId
));
57 GLC(context3d
, context3d
->texParameteri(Extensions3D::TEXTURE_RECTANGLE_ARB
, GraphicsContext3D::TEXTURE_MIN_FILTER
, GraphicsContext3D::LINEAR
));
58 GLC(context3d
, context3d
->texParameteri(Extensions3D::TEXTURE_RECTANGLE_ARB
, GraphicsContext3D::TEXTURE_MAG_FILTER
, GraphicsContext3D::LINEAR
));
59 GLC(context3d
, context3d
->texParameteri(Extensions3D::TEXTURE_RECTANGLE_ARB
, GraphicsContext3D::TEXTURE_WRAP_S
, GraphicsContext3D::CLAMP_TO_EDGE
));
60 GLC(context3d
, context3d
->texParameteri(Extensions3D::TEXTURE_RECTANGLE_ARB
, GraphicsContext3D::TEXTURE_WRAP_T
, GraphicsContext3D::CLAMP_TO_EDGE
));
61 context3d
->texImageIOSurface2DCHROMIUM(Extensions3D::TEXTURE_RECTANGLE_ARB
,
62 m_ioSurfaceSize
.width(),
63 m_ioSurfaceSize
.height(),
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
72 m_ioSurfaceChanged
= false;
76 void CCIOSurfaceLayerImpl::appendQuads(CCQuadSink
& quadSink
, CCAppendQuadsData
& appendQuadsData
)
78 CCSharedQuadState
* sharedQuadState
= quadSink
.useSharedQuadState(createSharedQuadState());
79 appendDebugBorderQuad(quadSink
, sharedQuadState
, appendQuadsData
);
81 IntRect
quadRect(IntPoint(), contentBounds());
82 quadSink
.append(CCIOSurfaceDrawQuad::create(sharedQuadState
, quadRect
, m_ioSurfaceSize
, m_ioSurfaceTextureId
, CCIOSurfaceDrawQuad::Flipped
).PassAs
<CCDrawQuad
>(), appendQuadsData
);
85 void CCIOSurfaceLayerImpl::dumpLayerProperties(std::string
* str
, int indent
) const
87 str
->append(indentString(indent
));
88 base::StringAppendF(str
, "iosurface id: %u texture id: %u\n", m_ioSurfaceId
, m_ioSurfaceTextureId
);
89 CCLayerImpl::dumpLayerProperties(str
, indent
);
92 void CCIOSurfaceLayerImpl::didLoseContext()
94 // We don't have a valid texture ID in the new context; however,
95 // the IOSurface is still valid.
96 m_ioSurfaceTextureId
= 0;
97 m_ioSurfaceChanged
= true;
100 void CCIOSurfaceLayerImpl::setIOSurfaceProperties(unsigned ioSurfaceId
, const IntSize
& size
)
102 if (m_ioSurfaceId
!= ioSurfaceId
)
103 m_ioSurfaceChanged
= true;
105 m_ioSurfaceId
= ioSurfaceId
;
106 m_ioSurfaceSize
= size
;
109 const char* CCIOSurfaceLayerImpl::layerTypeAsString() const
111 return "IOSurfaceLayer";