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/layers/io_surface_layer_impl.h"
7 #include "base/strings/stringprintf.h"
8 #include "cc/output/gl_renderer.h" // For the GLC() macro.
9 #include "cc/output/output_surface.h"
10 #include "cc/quads/io_surface_draw_quad.h"
11 #include "cc/trees/layer_tree_impl.h"
12 #include "cc/trees/occlusion_tracker.h"
13 #include "gpu/GLES2/gl2extchromium.h"
14 #include "gpu/command_buffer/client/gles2_interface.h"
15 #include "third_party/khronos/GLES2/gl2.h"
16 #include "third_party/khronos/GLES2/gl2ext.h"
20 IOSurfaceLayerImpl::IOSurfaceLayerImpl(LayerTreeImpl
* tree_impl
, int id
)
21 : LayerImpl(tree_impl
, id
),
23 io_surface_changed_(false),
24 io_surface_resource_id_(0) {}
26 IOSurfaceLayerImpl::~IOSurfaceLayerImpl() {
30 void IOSurfaceLayerImpl::DestroyResource() {
31 if (io_surface_resource_id_
) {
32 ResourceProvider
* resource_provider
=
33 layer_tree_impl()->resource_provider();
34 resource_provider
->DeleteResource(io_surface_resource_id_
);
35 io_surface_resource_id_
= 0;
39 scoped_ptr
<LayerImpl
> IOSurfaceLayerImpl::CreateLayerImpl(
40 LayerTreeImpl
* tree_impl
) {
41 return IOSurfaceLayerImpl::Create(tree_impl
, id()).PassAs
<LayerImpl
>();
44 void IOSurfaceLayerImpl::PushPropertiesTo(LayerImpl
* layer
) {
45 LayerImpl::PushPropertiesTo(layer
);
47 IOSurfaceLayerImpl
* io_surface_layer
=
48 static_cast<IOSurfaceLayerImpl
*>(layer
);
49 io_surface_layer
->SetIOSurfaceProperties(io_surface_id_
, io_surface_size_
);
52 bool IOSurfaceLayerImpl::WillDraw(DrawMode draw_mode
,
53 ResourceProvider
* resource_provider
) {
54 if (draw_mode
!= DRAW_MODE_HARDWARE
)
57 if (io_surface_changed_
) {
59 io_surface_resource_id_
= resource_provider
->CreateResourceFromIOSurface(
60 io_surface_size_
, io_surface_id_
);
61 io_surface_changed_
= false;
64 return LayerImpl::WillDraw(draw_mode
, resource_provider
);
67 void IOSurfaceLayerImpl::AppendQuads(
68 RenderPass
* render_pass
,
69 const OcclusionTracker
<LayerImpl
>& occlusion_tracker
,
70 AppendQuadsData
* append_quads_data
) {
71 SharedQuadState
* shared_quad_state
=
72 render_pass
->CreateAndAppendSharedQuadState();
73 PopulateSharedQuadState(shared_quad_state
);
75 AppendDebugBorderQuad(
76 render_pass
, content_bounds(), shared_quad_state
, append_quads_data
);
78 gfx::Rect
quad_rect(content_bounds());
79 gfx::Rect
opaque_rect(contents_opaque() ? quad_rect
: gfx::Rect());
80 gfx::Rect visible_quad_rect
= occlusion_tracker
.UnoccludedContentRect(
81 quad_rect
, draw_properties().target_space_transform
);
82 if (visible_quad_rect
.IsEmpty())
85 IOSurfaceDrawQuad
* quad
=
86 render_pass
->CreateAndAppendDrawQuad
<IOSurfaceDrawQuad
>();
87 quad
->SetNew(shared_quad_state
,
92 io_surface_resource_id_
,
93 IOSurfaceDrawQuad::FLIPPED
);
96 void IOSurfaceLayerImpl::ReleaseResources() {
97 // We don't have a valid resource ID in the new context; however,
98 // the IOSurface is still valid.
100 io_surface_changed_
= true;
103 void IOSurfaceLayerImpl::SetIOSurfaceProperties(unsigned io_surface_id
,
104 const gfx::Size
& size
) {
105 if (io_surface_id_
!= io_surface_id
)
106 io_surface_changed_
= true;
108 io_surface_id_
= io_surface_id
;
109 io_surface_size_
= size
;
112 const char* IOSurfaceLayerImpl::LayerTypeAsString() const {
113 return "cc::IOSurfaceLayerImpl";