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/output/delegating_renderer.h"
11 #include "base/debug/trace_event.h"
12 #include "base/strings/string_split.h"
13 #include "base/strings/string_util.h"
14 #include "cc/output/compositor_frame_ack.h"
15 #include "cc/quads/checkerboard_draw_quad.h"
16 #include "cc/quads/debug_border_draw_quad.h"
17 #include "cc/quads/render_pass.h"
18 #include "cc/quads/render_pass_draw_quad.h"
19 #include "cc/quads/solid_color_draw_quad.h"
20 #include "cc/quads/texture_draw_quad.h"
21 #include "cc/quads/tile_draw_quad.h"
22 #include "cc/quads/yuv_video_draw_quad.h"
23 #include "cc/resources/resource_provider.h"
24 #include "third_party/khronos/GLES2/gl2ext.h"
26 using WebKit::WebGraphicsContext3D
;
30 scoped_ptr
<DelegatingRenderer
> DelegatingRenderer::Create(
31 RendererClient
* client
,
32 OutputSurface
* output_surface
,
33 ResourceProvider
* resource_provider
) {
34 scoped_ptr
<DelegatingRenderer
> renderer(
35 new DelegatingRenderer(client
, output_surface
, resource_provider
));
36 if (!renderer
->Initialize())
37 return scoped_ptr
<DelegatingRenderer
>();
38 return renderer
.Pass();
41 DelegatingRenderer::DelegatingRenderer(
42 RendererClient
* client
,
43 OutputSurface
* output_surface
,
44 ResourceProvider
* resource_provider
)
46 output_surface_(output_surface
),
47 resource_provider_(resource_provider
),
49 DCHECK(resource_provider_
);
52 bool DelegatingRenderer::Initialize() {
53 capabilities_
.using_partial_swap
= false;
54 // TODO(danakj): Throttling - we may want to only allow 1 outstanding frame,
55 // but the parent compositor may pipeline for us.
56 capabilities_
.max_texture_size
= resource_provider_
->max_texture_size();
57 capabilities_
.best_texture_format
= resource_provider_
->best_texture_format();
58 capabilities_
.allow_partial_texture_updates
= false;
59 capabilities_
.using_offscreen_context3d
= false;
61 WebGraphicsContext3D
* context3d
= resource_provider_
->GraphicsContext3D();
64 // Software compositing.
68 if (!context3d
->makeContextCurrent())
71 context3d
->pushGroupMarkerEXT("CompositorContext");
73 std::string extensions_string
=
74 UTF16ToASCII(context3d
->getString(GL_EXTENSIONS
));
76 std::vector
<std::string
> extensions
;
77 base::SplitString(extensions_string
, ' ', &extensions
);
79 // TODO(danakj): We need non-GPU-specific paths for these things. This
80 // renderer shouldn't need to use context3d extensions directly.
81 bool has_set_visibility
= false;
82 bool has_io_surface
= false;
83 bool has_arb_texture_rect
= false;
84 bool has_egl_image
= false;
85 bool has_map_image
= false;
86 for (size_t i
= 0; i
< extensions
.size(); ++i
) {
87 if (extensions
[i
] == "GL_CHROMIUM_set_visibility") {
88 has_set_visibility
= true;
89 } else if (extensions
[i
] == "GL_CHROMIUM_iosurface") {
90 has_io_surface
= true;
91 } else if (extensions
[i
] == "GL_ARB_texture_rectangle") {
92 has_arb_texture_rect
= true;
93 } else if (extensions
[i
] == "GL_OES_EGL_image_external") {
95 } else if (extensions
[i
] == "GL_CHROMIUM_map_image") {
101 DCHECK(has_arb_texture_rect
);
103 // TODO(piman): loop visibility to GPU process?
104 capabilities_
.using_set_visibility
= has_set_visibility
;
106 // TODO(danakj): Support GpuMemoryManager.
107 capabilities_
.using_gpu_memory_manager
= false;
109 capabilities_
.using_egl_image
= has_egl_image
;
111 capabilities_
.using_map_image
= has_map_image
;
116 DelegatingRenderer::~DelegatingRenderer() {}
118 const RendererCapabilities
& DelegatingRenderer::Capabilities() const {
119 return capabilities_
;
122 bool DelegatingRenderer::CanReadPixels() const { return false; }
124 static ResourceProvider::ResourceId
AppendToArray(
125 ResourceProvider::ResourceIdArray
* array
,
126 ResourceProvider::ResourceId id
) {
127 array
->push_back(id
);
131 void DelegatingRenderer::DrawFrame(
132 RenderPassList
* render_passes_in_draw_order
) {
133 TRACE_EVENT0("cc", "DelegatingRenderer::DrawFrame");
135 DCHECK(!frame_for_swap_buffers_
.delegated_frame_data
);
137 frame_for_swap_buffers_
.metadata
= client_
->MakeCompositorFrameMetadata();
139 frame_for_swap_buffers_
.delegated_frame_data
=
140 make_scoped_ptr(new DelegatedFrameData
);
141 DelegatedFrameData
& out_data
= *frame_for_swap_buffers_
.delegated_frame_data
;
142 // Move the render passes and resources into the |out_frame|.
143 out_data
.render_pass_list
.swap(*render_passes_in_draw_order
);
145 // Collect all resource ids in the render passes into a ResourceIdArray.
146 ResourceProvider::ResourceIdArray resources
;
147 DrawQuad::ResourceIteratorCallback append_to_array
=
148 base::Bind(&AppendToArray
, &resources
);
149 for (size_t i
= 0; i
< out_data
.render_pass_list
.size(); ++i
) {
150 RenderPass
* render_pass
= out_data
.render_pass_list
.at(i
);
151 for (size_t j
= 0; j
< render_pass
->quad_list
.size(); ++j
)
152 render_pass
->quad_list
[j
]->IterateResources(append_to_array
);
154 resource_provider_
->PrepareSendToParent(resources
, &out_data
.resource_list
);
157 void DelegatingRenderer::SwapBuffers() {
158 TRACE_EVENT0("cc", "DelegatingRenderer::SwapBuffers");
160 output_surface_
->SwapBuffers(&frame_for_swap_buffers_
);
161 frame_for_swap_buffers_
.delegated_frame_data
.reset();
164 void DelegatingRenderer::GetFramebufferPixels(void* pixels
, gfx::Rect rect
) {
168 void DelegatingRenderer::ReceiveSwapBuffersAck(
169 const CompositorFrameAck
& ack
) {
170 resource_provider_
->ReceiveFromParent(ack
.resources
);
174 bool DelegatingRenderer::IsContextLost() {
175 WebGraphicsContext3D
* context3d
= resource_provider_
->GraphicsContext3D();
178 return context3d
->getGraphicsResetStatusARB() != GL_NO_ERROR
;
181 void DelegatingRenderer::SetVisible(bool visible
) {