IndexedDB: fsync after transactions.
[chromium-blink-merge.git] / cc / output / delegating_renderer.cc
blobe56d1465a1683728d6b607d68f6ca6c961754df5
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"
7 #include <set>
8 #include <string>
9 #include <vector>
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"
31 namespace cc {
33 scoped_ptr<DelegatingRenderer> DelegatingRenderer::Create(
34 RendererClient* client,
35 const LayerTreeSettings* settings,
36 OutputSurface* output_surface,
37 ResourceProvider* resource_provider) {
38 scoped_ptr<DelegatingRenderer> renderer(new DelegatingRenderer(
39 client, settings, output_surface, resource_provider));
40 if (!renderer->Initialize())
41 return scoped_ptr<DelegatingRenderer>();
42 return renderer.Pass();
45 DelegatingRenderer::DelegatingRenderer(RendererClient* client,
46 const LayerTreeSettings* settings,
47 OutputSurface* output_surface,
48 ResourceProvider* resource_provider)
49 : Renderer(client, settings),
50 output_surface_(output_surface),
51 resource_provider_(resource_provider),
52 visible_(true) {
53 DCHECK(resource_provider_);
56 bool DelegatingRenderer::Initialize() {
57 capabilities_.using_partial_swap = false;
58 capabilities_.max_texture_size = resource_provider_->max_texture_size();
59 capabilities_.best_texture_format = resource_provider_->best_texture_format();
60 capabilities_.allow_partial_texture_updates = false;
61 capabilities_.using_offscreen_context3d = false;
63 if (!output_surface_->context_provider()) {
64 capabilities_.using_shared_memory_resources = true;
65 capabilities_.using_map_image = settings_->use_map_image;
66 return true;
69 const ContextProvider::Capabilities& caps =
70 output_surface_->context_provider()->ContextCapabilities();
72 DCHECK(!caps.gpu.iosurface || caps.gpu.texture_rectangle);
74 capabilities_.using_egl_image = caps.gpu.egl_image_external;
75 capabilities_.using_map_image =
76 settings_->use_map_image && caps.gpu.map_image;
78 return true;
81 DelegatingRenderer::~DelegatingRenderer() {}
83 const RendererCapabilitiesImpl& DelegatingRenderer::Capabilities() const {
84 return capabilities_;
87 bool DelegatingRenderer::CanReadPixels() const { return false; }
89 static ResourceProvider::ResourceId AppendToArray(
90 ResourceProvider::ResourceIdArray* array,
91 ResourceProvider::ResourceId id) {
92 array->push_back(id);
93 return id;
96 void DelegatingRenderer::DrawFrame(RenderPassList* render_passes_in_draw_order,
97 ContextProvider* offscreen_context_provider,
98 float device_scale_factor,
99 const gfx::Rect& device_viewport_rect,
100 const gfx::Rect& device_clip_rect,
101 bool allow_partial_swap,
102 bool disable_picture_quad_image_filtering) {
103 TRACE_EVENT0("cc", "DelegatingRenderer::DrawFrame");
105 DCHECK(!delegated_frame_data_);
107 delegated_frame_data_ = make_scoped_ptr(new DelegatedFrameData);
108 DelegatedFrameData& out_data = *delegated_frame_data_;
109 // Move the render passes and resources into the |out_frame|.
110 out_data.render_pass_list.swap(*render_passes_in_draw_order);
112 // Collect all resource ids in the render passes into a ResourceIdArray.
113 ResourceProvider::ResourceIdArray resources;
114 DrawQuad::ResourceIteratorCallback append_to_array =
115 base::Bind(&AppendToArray, &resources);
116 for (size_t i = 0; i < out_data.render_pass_list.size(); ++i) {
117 RenderPass* render_pass = out_data.render_pass_list.at(i);
118 for (size_t j = 0; j < render_pass->quad_list.size(); ++j)
119 render_pass->quad_list[j]->IterateResources(append_to_array);
121 resource_provider_->PrepareSendToParent(resources, &out_data.resource_list);
124 void DelegatingRenderer::SwapBuffers(const CompositorFrameMetadata& metadata) {
125 TRACE_EVENT0("cc", "DelegatingRenderer::SwapBuffers");
126 CompositorFrame compositor_frame;
127 compositor_frame.metadata = metadata;
128 compositor_frame.delegated_frame_data = delegated_frame_data_.Pass();
129 output_surface_->SwapBuffers(&compositor_frame);
132 void DelegatingRenderer::GetFramebufferPixels(void* pixels,
133 const gfx::Rect& rect) {
134 NOTREACHED();
137 void DelegatingRenderer::ReceiveSwapBuffersAck(
138 const CompositorFrameAck& ack) {
139 resource_provider_->ReceiveReturnsFromParent(ack.resources);
142 bool DelegatingRenderer::IsContextLost() {
143 ContextProvider* context_provider = output_surface_->context_provider();
144 if (!context_provider)
145 return false;
146 return context_provider->IsContextLost();
149 void DelegatingRenderer::SetVisible(bool visible) {
150 if (visible == visible_)
151 return;
153 visible_ = visible;
154 ContextProvider* context_provider = output_surface_->context_provider();
155 if (!visible_) {
156 TRACE_EVENT0("cc", "DelegatingRenderer::SetVisible dropping resources");
157 resource_provider_->ReleaseCachedData();
158 if (context_provider)
159 context_provider->ContextGL()->Flush();
161 // We loop visibility to the GPU process, since that's what manages memory.
162 // That will allow it to feed us with memory allocations that we can act
163 // upon.
164 DCHECK(context_provider);
165 context_provider->ContextSupport()->SetSurfaceVisible(visible);
168 void DelegatingRenderer::SendManagedMemoryStats(size_t bytes_visible,
169 size_t bytes_visible_and_nearby,
170 size_t bytes_allocated) {
171 ContextProvider* context_provider = output_surface_->context_provider();
172 if (!context_provider) {
173 // TODO(piman): software path.
174 NOTIMPLEMENTED();
175 return;
177 gpu::ManagedMemoryStats stats;
178 stats.bytes_required = bytes_visible;
179 stats.bytes_nice_to_have = bytes_visible_and_nearby;
180 stats.bytes_allocated = bytes_allocated;
181 stats.backbuffer_requested = false;
183 context_provider->ContextSupport()->SendManagedMemoryStats(stats);
186 } // namespace cc