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 "base/strings/stringprintf.h"
15 #include "cc/output/compositor_frame_ack.h"
16 #include "cc/quads/checkerboard_draw_quad.h"
17 #include "cc/quads/debug_border_draw_quad.h"
18 #include "cc/quads/render_pass.h"
19 #include "cc/quads/render_pass_draw_quad.h"
20 #include "cc/quads/solid_color_draw_quad.h"
21 #include "cc/quads/texture_draw_quad.h"
22 #include "cc/quads/tile_draw_quad.h"
23 #include "cc/quads/yuv_video_draw_quad.h"
24 #include "cc/resources/resource_provider.h"
25 #include "gpu/command_buffer/client/context_support.h"
26 #include "gpu/command_buffer/client/gles2_interface.h"
27 #include "gpu/command_buffer/common/gpu_memory_allocation.h"
28 #include "third_party/khronos/GLES2/gl2ext.h"
33 scoped_ptr
<DelegatingRenderer
> DelegatingRenderer::Create(
34 RendererClient
* client
,
35 const LayerTreeSettings
* settings
,
36 OutputSurface
* output_surface
,
37 ResourceProvider
* resource_provider
) {
38 return make_scoped_ptr(new DelegatingRenderer(
39 client
, settings
, output_surface
, resource_provider
));
42 DelegatingRenderer::DelegatingRenderer(RendererClient
* client
,
43 const LayerTreeSettings
* settings
,
44 OutputSurface
* output_surface
,
45 ResourceProvider
* resource_provider
)
46 : Renderer(client
, settings
),
47 output_surface_(output_surface
),
48 resource_provider_(resource_provider
) {
49 DCHECK(resource_provider_
);
51 capabilities_
.using_partial_swap
= false;
52 capabilities_
.max_texture_size
= resource_provider_
->max_texture_size();
53 capabilities_
.best_texture_format
= resource_provider_
->best_texture_format();
54 capabilities_
.allow_partial_texture_updates
= false;
56 if (!output_surface_
->context_provider()) {
57 capabilities_
.using_shared_memory_resources
= true;
58 capabilities_
.using_map_image
= true;
60 const ContextProvider::Capabilities
& caps
=
61 output_surface_
->context_provider()->ContextCapabilities();
63 DCHECK(!caps
.gpu
.iosurface
|| caps
.gpu
.texture_rectangle
);
65 capabilities_
.using_egl_image
= caps
.gpu
.egl_image_external
;
66 capabilities_
.using_map_image
= caps
.gpu
.map_image
;
68 capabilities_
.allow_rasterize_on_demand
= false;
72 DelegatingRenderer::~DelegatingRenderer() {}
74 const RendererCapabilitiesImpl
& DelegatingRenderer::Capabilities() const {
78 bool DelegatingRenderer::CanReadPixels() const { return false; }
80 static ResourceProvider::ResourceId
AppendToArray(
81 ResourceProvider::ResourceIdArray
* array
,
82 ResourceProvider::ResourceId id
) {
87 void DelegatingRenderer::DrawFrame(RenderPassList
* render_passes_in_draw_order
,
88 float device_scale_factor
,
89 const gfx::Rect
& device_viewport_rect
,
90 const gfx::Rect
& device_clip_rect
,
91 bool disable_picture_quad_image_filtering
) {
92 TRACE_EVENT0("cc", "DelegatingRenderer::DrawFrame");
94 DCHECK(!delegated_frame_data_
);
96 delegated_frame_data_
= make_scoped_ptr(new DelegatedFrameData
);
97 DelegatedFrameData
& out_data
= *delegated_frame_data_
;
98 // Move the render passes and resources into the |out_frame|.
99 out_data
.render_pass_list
.swap(*render_passes_in_draw_order
);
101 // Collect all resource ids in the render passes into a ResourceIdArray.
102 ResourceProvider::ResourceIdArray resources
;
103 DrawQuad::ResourceIteratorCallback append_to_array
=
104 base::Bind(&AppendToArray
, &resources
);
105 for (size_t i
= 0; i
< out_data
.render_pass_list
.size(); ++i
) {
106 RenderPass
* render_pass
= out_data
.render_pass_list
.at(i
);
107 for (size_t j
= 0; j
< render_pass
->quad_list
.size(); ++j
)
108 render_pass
->quad_list
[j
]->IterateResources(append_to_array
);
110 resource_provider_
->PrepareSendToParent(resources
, &out_data
.resource_list
);
113 void DelegatingRenderer::SwapBuffers(const CompositorFrameMetadata
& metadata
) {
114 TRACE_EVENT0("cc,benchmark", "DelegatingRenderer::SwapBuffers");
115 CompositorFrame compositor_frame
;
116 compositor_frame
.metadata
= metadata
;
117 compositor_frame
.delegated_frame_data
= delegated_frame_data_
.Pass();
118 output_surface_
->SwapBuffers(&compositor_frame
);
121 void DelegatingRenderer::GetFramebufferPixels(void* pixels
,
122 const gfx::Rect
& rect
) {
126 void DelegatingRenderer::ReceiveSwapBuffersAck(
127 const CompositorFrameAck
& ack
) {
128 resource_provider_
->ReceiveReturnsFromParent(ack
.resources
);
131 bool DelegatingRenderer::IsContextLost() {
132 ContextProvider
* context_provider
= output_surface_
->context_provider();
133 if (!context_provider
)
135 return context_provider
->IsContextLost();
138 void DelegatingRenderer::DidChangeVisibility() {
139 ContextProvider
* context_provider
= output_surface_
->context_provider();
141 TRACE_EVENT0("cc", "DelegatingRenderer::SetVisible dropping resources");
142 resource_provider_
->ReleaseCachedData();
143 if (context_provider
) {
144 context_provider
->DeleteCachedResources();
145 context_provider
->ContextGL()->Flush();
148 // We loop visibility to the GPU process, since that's what manages memory.
149 // That will allow it to feed us with memory allocations that we can act
151 if (context_provider
)
152 context_provider
->ContextSupport()->SetSurfaceVisible(visible());
155 void DelegatingRenderer::SendManagedMemoryStats(size_t bytes_visible
,
156 size_t bytes_visible_and_nearby
,
157 size_t bytes_allocated
) {
158 ContextProvider
* context_provider
= output_surface_
->context_provider();
159 if (!context_provider
) {
160 // In the software path each child process manages its memory separately,
161 // so memory stats don't have to be sent anywhere.
164 gpu::ManagedMemoryStats stats
;
165 stats
.bytes_required
= bytes_visible
;
166 stats
.bytes_nice_to_have
= bytes_visible_and_nearby
;
167 stats
.bytes_allocated
= bytes_allocated
;
168 stats
.backbuffer_requested
= false;
170 context_provider
->ContextSupport()->SendManagedMemoryStats(stats
);