Update To 11.40.268.0
[platform/framework/web/crosswalk.git] / src / cc / layers / io_surface_layer_impl.cc
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/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.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
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
26 IOSurfaceLayerImpl::~IOSurfaceLayerImpl() {
27   DestroyResource();
28 }
29
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
39 scoped_ptr<LayerImpl> IOSurfaceLayerImpl::CreateLayerImpl(
40     LayerTreeImpl* tree_impl) {
41   return IOSurfaceLayerImpl::Create(tree_impl, id());
42 }
43
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
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
67 void IOSurfaceLayerImpl::AppendQuads(
68     RenderPass* render_pass,
69     const Occlusion& occlusion_in_content_space,
70     AppendQuadsData* append_quads_data) {
71   SharedQuadState* shared_quad_state =
72       render_pass->CreateAndAppendSharedQuadState();
73   PopulateSharedQuadState(shared_quad_state);
74
75   AppendDebugBorderQuad(
76       render_pass, content_bounds(), shared_quad_state, append_quads_data);
77
78   gfx::Rect quad_rect(content_bounds());
79   gfx::Rect opaque_rect(contents_opaque() ? quad_rect : gfx::Rect());
80   gfx::Rect visible_quad_rect =
81       occlusion_in_content_space.GetUnoccludedContentRect(quad_rect);
82   if (visible_quad_rect.IsEmpty())
83     return;
84
85   IOSurfaceDrawQuad* quad =
86       render_pass->CreateAndAppendDrawQuad<IOSurfaceDrawQuad>();
87   quad->SetNew(shared_quad_state,
88                quad_rect,
89                opaque_rect,
90                visible_quad_rect,
91                io_surface_size_,
92                io_surface_resource_id_,
93                IOSurfaceDrawQuad::FLIPPED);
94 }
95
96 void IOSurfaceLayerImpl::ReleaseResources() {
97   // We don't have a valid resource ID in the new context; however,
98   // the IOSurface is still valid.
99   DestroyResource();
100   io_surface_changed_ = true;
101 }
102
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;
107
108   io_surface_id_ = io_surface_id;
109   io_surface_size_ = size;
110 }
111
112 const char* IOSurfaceLayerImpl::LayerTypeAsString() const {
113   return "cc::IOSurfaceLayerImpl";
114 }
115
116 }  // namespace cc