GPU workaround to simulate Out of Memory errors with large textures
[chromium-blink-merge.git] / content / common / gpu / image_transport_surface.cc
blobb6367dd4cd8b7e74f153726f5b4a3e7d4b179170
1 // Copyright (c) 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 "content/common/gpu/image_transport_surface.h"
7 #include "base/bind.h"
8 #include "base/bind_helpers.h"
9 #include "base/command_line.h"
10 #include "base/trace_event/trace_event.h"
11 #include "content/common/gpu/gpu_channel.h"
12 #include "content/common/gpu/gpu_channel_manager.h"
13 #include "content/common/gpu/gpu_command_buffer_stub.h"
14 #include "content/common/gpu/gpu_messages.h"
15 #include "content/common/gpu/null_transport_surface.h"
16 #include "gpu/command_buffer/service/sync_point_manager.h"
17 #include "ui/gfx/vsync_provider.h"
18 #include "ui/gl/gl_implementation.h"
19 #include "ui/gl/gl_switches.h"
21 namespace content {
23 ImageTransportSurface::ImageTransportSurface() {}
25 ImageTransportSurface::~ImageTransportSurface() {}
27 scoped_refptr<gfx::GLSurface> ImageTransportSurface::CreateSurface(
28 GpuChannelManager* manager,
29 GpuCommandBufferStub* stub,
30 const gfx::GLSurfaceHandle& handle) {
31 scoped_refptr<gfx::GLSurface> surface;
32 if (handle.transport_type == gfx::NULL_TRANSPORT) {
33 #if defined(OS_ANDROID)
34 surface = CreateTransportSurface(manager, stub, handle);
35 #else
36 surface = new NullTransportSurface(manager, stub, handle);
37 #endif
38 } else {
39 surface = CreateNativeSurface(manager, stub, handle);
42 if (!surface.get() || !surface->Initialize())
43 return NULL;
44 return surface;
47 ImageTransportHelper::ImageTransportHelper(ImageTransportSurface* surface,
48 GpuChannelManager* manager,
49 GpuCommandBufferStub* stub,
50 gfx::PluginWindowHandle handle)
51 : surface_(surface),
52 manager_(manager),
53 stub_(stub->AsWeakPtr()),
54 handle_(handle) {
55 route_id_ = manager_->GenerateRouteID();
56 manager_->AddRoute(route_id_, this);
59 ImageTransportHelper::~ImageTransportHelper() {
60 if (stub_.get()) {
61 stub_->SetLatencyInfoCallback(
62 base::Callback<void(const std::vector<ui::LatencyInfo>&)>());
64 manager_->RemoveRoute(route_id_);
67 bool ImageTransportHelper::Initialize() {
68 gpu::gles2::GLES2Decoder* decoder = Decoder();
70 if (!decoder)
71 return false;
73 decoder->SetResizeCallback(
74 base::Bind(&ImageTransportHelper::Resize, base::Unretained(this)));
76 stub_->SetLatencyInfoCallback(
77 base::Bind(&ImageTransportHelper::SetLatencyInfo,
78 base::Unretained(this)));
80 manager_->Send(new GpuHostMsg_AcceleratedSurfaceInitialized(
81 stub_->surface_id(), route_id_));
83 return true;
86 bool ImageTransportHelper::OnMessageReceived(const IPC::Message& message) {
87 bool handled = true;
88 IPC_BEGIN_MESSAGE_MAP(ImageTransportHelper, message)
89 #if defined(OS_MACOSX)
90 IPC_MESSAGE_HANDLER(AcceleratedSurfaceMsg_BufferPresented,
91 OnBufferPresented)
92 #endif
93 IPC_MESSAGE_HANDLER(AcceleratedSurfaceMsg_WakeUpGpu, OnWakeUpGpu);
94 IPC_MESSAGE_UNHANDLED(handled = false)
95 IPC_END_MESSAGE_MAP()
96 return handled;
99 void ImageTransportHelper::SendAcceleratedSurfaceBuffersSwapped(
100 GpuHostMsg_AcceleratedSurfaceBuffersSwapped_Params params) {
101 // TRACE_EVENT for gpu tests:
102 TRACE_EVENT_INSTANT2("test_gpu", "SwapBuffers",
103 TRACE_EVENT_SCOPE_THREAD,
104 "GLImpl", static_cast<int>(gfx::GetGLImplementation()),
105 "width", params.size.width());
106 params.surface_id = stub_->surface_id();
107 params.route_id = route_id_;
108 manager_->Send(new GpuHostMsg_AcceleratedSurfaceBuffersSwapped(params));
111 void ImageTransportHelper::SetPreemptByFlag(
112 scoped_refptr<gpu::PreemptionFlag> preemption_flag) {
113 stub_->channel()->SetPreemptByFlag(preemption_flag);
116 bool ImageTransportHelper::MakeCurrent() {
117 gpu::gles2::GLES2Decoder* decoder = Decoder();
118 if (!decoder)
119 return false;
120 return decoder->MakeCurrent();
123 void ImageTransportHelper::SetSwapInterval(gfx::GLContext* context) {
124 if (base::CommandLine::ForCurrentProcess()->HasSwitch(
125 switches::kDisableGpuVsync))
126 context->ForceSwapIntervalZero(true);
127 else
128 context->SetSwapInterval(1);
131 gpu::gles2::GLES2Decoder* ImageTransportHelper::Decoder() {
132 if (!stub_.get())
133 return NULL;
134 return stub_->decoder();
137 #if defined(OS_MACOSX)
138 void ImageTransportHelper::OnBufferPresented(
139 const AcceleratedSurfaceMsg_BufferPresented_Params& params) {
140 surface_->OnBufferPresented(params);
142 #endif
144 void ImageTransportHelper::OnWakeUpGpu() {
145 surface_->WakeUpGpu();
148 void ImageTransportHelper::Resize(gfx::Size size, float scale_factor) {
149 surface_->OnResize(size, scale_factor);
151 #if defined(OS_ANDROID)
152 manager_->gpu_memory_manager()->ScheduleManage(
153 GpuMemoryManager::kScheduleManageNow);
154 #endif
157 void ImageTransportHelper::SetLatencyInfo(
158 const std::vector<ui::LatencyInfo>& latency_info) {
159 surface_->SetLatencyInfo(latency_info);
162 PassThroughImageTransportSurface::PassThroughImageTransportSurface(
163 GpuChannelManager* manager,
164 GpuCommandBufferStub* stub,
165 gfx::GLSurface* surface)
166 : GLSurfaceAdapter(surface),
167 did_set_swap_interval_(false),
168 weak_ptr_factory_(this) {
169 helper_.reset(new ImageTransportHelper(this,
170 manager,
171 stub,
172 gfx::kNullPluginWindow));
175 bool PassThroughImageTransportSurface::Initialize() {
176 // The surface is assumed to have already been initialized.
177 return helper_->Initialize();
180 void PassThroughImageTransportSurface::Destroy() {
181 GLSurfaceAdapter::Destroy();
184 void PassThroughImageTransportSurface::SetLatencyInfo(
185 const std::vector<ui::LatencyInfo>& latency_info) {
186 for (size_t i = 0; i < latency_info.size(); i++)
187 latency_info_.push_back(latency_info[i]);
190 bool PassThroughImageTransportSurface::SwapBuffers() {
191 // GetVsyncValues before SwapBuffers to work around Mali driver bug:
192 // crbug.com/223558.
193 SendVSyncUpdateIfAvailable();
195 base::TimeTicks swap_time = base::TimeTicks::Now();
196 for (auto& latency : latency_info_) {
197 latency.AddLatencyNumberWithTimestamp(
198 ui::INPUT_EVENT_GPU_SWAP_BUFFER_COMPONENT, 0, 0, swap_time, 1);
201 // We use WeakPtr here to avoid manual management of life time of an instance
202 // of this class. Callback will not be called once the instance of this class
203 // is destroyed. However, this also means that the callback can be run on
204 // the calling thread only.
205 return gfx::GLSurfaceAdapter::SwapBuffersAsync(
206 base::Bind(&PassThroughImageTransportSurface::SwapBuffersCallBack,
207 weak_ptr_factory_.GetWeakPtr()));
210 bool PassThroughImageTransportSurface::PostSubBuffer(
211 int x, int y, int width, int height) {
212 SendVSyncUpdateIfAvailable();
214 base::TimeTicks swap_time = base::TimeTicks::Now();
215 for (auto& latency : latency_info_) {
216 latency.AddLatencyNumberWithTimestamp(
217 ui::INPUT_EVENT_GPU_SWAP_BUFFER_COMPONENT, 0, 0, swap_time, 1);
220 // We use WeakPtr here to avoid manual management of life time of an instance
221 // of this class. Callback will not be called once the instance of this class
222 // is destroyed. However, this also means that the callback can be run on
223 // the calling thread only.
224 return gfx::GLSurfaceAdapter::PostSubBufferAsync(x, y, width, height,
225 base::Bind(&PassThroughImageTransportSurface::SwapBuffersCallBack,
226 weak_ptr_factory_.GetWeakPtr()));
229 void PassThroughImageTransportSurface::SwapBuffersCallBack() {
230 base::TimeTicks swap_ack_time = base::TimeTicks::Now();
231 for (auto& latency : latency_info_) {
232 latency.AddLatencyNumberWithTimestamp(
233 ui::INPUT_EVENT_LATENCY_TERMINATED_FRAME_SWAP_COMPONENT, 0, 0,
234 swap_ack_time, 1);
237 helper_->stub()->SendSwapBuffersCompleted(latency_info_);
238 latency_info_.clear();
241 bool PassThroughImageTransportSurface::OnMakeCurrent(gfx::GLContext* context) {
242 if (!did_set_swap_interval_) {
243 ImageTransportHelper::SetSwapInterval(context);
244 did_set_swap_interval_ = true;
246 return true;
249 #if defined(OS_MACOSX)
250 void PassThroughImageTransportSurface::OnBufferPresented(
251 const AcceleratedSurfaceMsg_BufferPresented_Params& /* params */) {
252 NOTREACHED();
254 #endif
256 void PassThroughImageTransportSurface::OnResize(gfx::Size size,
257 float scale_factor) {
258 Resize(size);
261 gfx::Size PassThroughImageTransportSurface::GetSize() {
262 return GLSurfaceAdapter::GetSize();
265 void PassThroughImageTransportSurface::WakeUpGpu() {
266 NOTREACHED();
269 PassThroughImageTransportSurface::~PassThroughImageTransportSurface() {}
271 void PassThroughImageTransportSurface::SendVSyncUpdateIfAvailable() {
272 gfx::VSyncProvider* vsync_provider = GetVSyncProvider();
273 if (vsync_provider) {
274 vsync_provider->GetVSyncParameters(
275 base::Bind(&GpuCommandBufferStub::SendUpdateVSyncParameters,
276 helper_->stub()->AsWeakPtr()));
280 } // namespace content