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.
4
5 #include "cc/layers/io_surface_layer_impl.h"
6
7 #include "base/strings/stringprintf.h"
8 #include "cc/layers/quad_sink.h"
9 #include "cc/output/gl_renderer.h" // For the GLC() macro.
10 #include "cc/output/output_surface.h"
11 #include "cc/quads/io_surface_draw_quad.h"
12 #include "cc/trees/layer_tree_impl.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"
17
18 namespace cc {
19
IOSurfaceLayerImpl(LayerTreeImpl * tree_impl,int id)20 IOSurfaceLayerImpl::IOSurfaceLayerImpl(LayerTreeImpl* tree_impl, int id)
21 : LayerImpl(tree_impl, id),
22 io_surface_id_(0),
23 io_surface_changed_(false),
24 io_surface_resource_id_(0) {}
25
~IOSurfaceLayerImpl()26 IOSurfaceLayerImpl::~IOSurfaceLayerImpl() {
27 DestroyResource();
28 }
29
DestroyResource()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;
36 }
37 }
38
CreateLayerImpl(LayerTreeImpl * tree_impl)39 scoped_ptr<LayerImpl> IOSurfaceLayerImpl::CreateLayerImpl(
40 LayerTreeImpl* tree_impl) {
41 return IOSurfaceLayerImpl::Create(tree_impl, id()).PassAs<LayerImpl>();
42 }
43
PushPropertiesTo(LayerImpl * layer)44 void IOSurfaceLayerImpl::PushPropertiesTo(LayerImpl* layer) {
45 LayerImpl::PushPropertiesTo(layer);
46
47 IOSurfaceLayerImpl* io_surface_layer =
48 static_cast<IOSurfaceLayerImpl*>(layer);
49 io_surface_layer->SetIOSurfaceProperties(io_surface_id_, io_surface_size_);
50 }
51
WillDraw(DrawMode draw_mode,ResourceProvider * resource_provider)52 bool IOSurfaceLayerImpl::WillDraw(DrawMode draw_mode,
53 ResourceProvider* resource_provider) {
54 if (draw_mode != DRAW_MODE_HARDWARE)
55 return false;
56
57 if (io_surface_changed_) {
58 DestroyResource();
59 io_surface_resource_id_ = resource_provider->CreateResourceFromIOSurface(
60 io_surface_size_, io_surface_id_);
61 io_surface_changed_ = false;
62 }
63
64 return LayerImpl::WillDraw(draw_mode, resource_provider);
65 }
66
AppendQuads(QuadSink * quad_sink,AppendQuadsData * append_quads_data)67 void IOSurfaceLayerImpl::AppendQuads(QuadSink* quad_sink,
68 AppendQuadsData* append_quads_data) {
69 SharedQuadState* shared_quad_state = quad_sink->CreateSharedQuadState();
70 PopulateSharedQuadState(shared_quad_state);
71
72 AppendDebugBorderQuad(
73 quad_sink, content_bounds(), shared_quad_state, append_quads_data);
74
75 gfx::Rect quad_rect(content_bounds());
76 gfx::Rect opaque_rect(contents_opaque() ? quad_rect : gfx::Rect());
77 gfx::Rect visible_quad_rect = quad_sink->UnoccludedContentRect(
78 quad_rect, draw_properties().target_space_transform);
79 if (visible_quad_rect.IsEmpty())
80 return;
81
82 scoped_ptr<IOSurfaceDrawQuad> quad = IOSurfaceDrawQuad::Create();
83 quad->SetNew(shared_quad_state,
84 quad_rect,
85 opaque_rect,
86 visible_quad_rect,
87 io_surface_size_,
88 io_surface_resource_id_,
89 IOSurfaceDrawQuad::FLIPPED);
90 quad_sink->Append(quad.PassAs<DrawQuad>());
91 }
92
ReleaseResources()93 void IOSurfaceLayerImpl::ReleaseResources() {
94 // We don't have a valid resource ID in the new context; however,
95 // the IOSurface is still valid.
96 DestroyResource();
97 io_surface_changed_ = true;
98 }
99
SetIOSurfaceProperties(unsigned io_surface_id,const gfx::Size & size)100 void IOSurfaceLayerImpl::SetIOSurfaceProperties(unsigned io_surface_id,
101 const gfx::Size& size) {
102 if (io_surface_id_ != io_surface_id)
103 io_surface_changed_ = true;
104
105 io_surface_id_ = io_surface_id;
106 io_surface_size_ = size;
107 }
108
LayerTypeAsString() const109 const char* IOSurfaceLayerImpl::LayerTypeAsString() const {
110 return "cc::IOSurfaceLayerImpl";
111 }
112
113 } // namespace cc
114