summaryrefslogtreecommitdiffstats
path: root/gpu/command_buffer/service/gpu_scheduler.cc
blob: 53fcfa249f919ef41cc2b42f609b034cbbff2efa (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
// Copyright (c) 2011 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "gpu/command_buffer/service/gpu_scheduler.h"

#include "base/callback.h"
#include "base/command_line.h"
#include "base/compiler_specific.h"
#include "base/debug/trace_event.h"
#include "base/message_loop.h"
#include "base/time.h"
#include "ui/gfx/gl/gl_context.h"
#include "ui/gfx/gl/gl_bindings.h"
#include "ui/gfx/gl/gl_surface.h"
#include "ui/gfx/gl/gl_switches.h"

using ::base::SharedMemory;

namespace gpu {

GpuScheduler::GpuScheduler(CommandBuffer* command_buffer,
                           SurfaceManager* surface_manager,
                           gles2::ContextGroup* group)
    : command_buffer_(command_buffer),
      commands_per_update_(100),
      unscheduled_count_(0),
#if defined(OS_MACOSX)
      swap_buffers_count_(0),
      acknowledged_swap_buffers_count_(0),
#endif
      method_factory_(ALLOW_THIS_IN_INITIALIZER_LIST(this)) {
  DCHECK(command_buffer);
  decoder_.reset(gles2::GLES2Decoder::Create(surface_manager, group));
  decoder_->set_engine(this);
}

GpuScheduler::GpuScheduler(CommandBuffer* command_buffer,
                           gles2::GLES2Decoder* decoder,
                           CommandParser* parser,
                           int commands_per_update)
    : command_buffer_(command_buffer),
      commands_per_update_(commands_per_update),
      unscheduled_count_(0),
#if defined(OS_MACOSX)
      swap_buffers_count_(0),
      acknowledged_swap_buffers_count_(0),
#endif
      method_factory_(ALLOW_THIS_IN_INITIALIZER_LIST(this)) {
  DCHECK(command_buffer);
  decoder_.reset(decoder);
  parser_.reset(parser);
}

GpuScheduler::~GpuScheduler() {
  Destroy();
}

bool GpuScheduler::InitializeCommon(
    const scoped_refptr<gfx::GLSurface>& surface,
    const scoped_refptr<gfx::GLContext>& context,
    const gfx::Size& size,
    const gles2::DisallowedExtensions& disallowed_extensions,
    const char* allowed_extensions,
    const std::vector<int32>& attribs,
    gles2::GLES2Decoder* parent_decoder,
    uint32 parent_texture_id) {
  DCHECK(context);

  if (!context->MakeCurrent(surface))
    return false;

#if !defined(OS_MACOSX)
  // Set up swap interval for onscreen contexts.
  if (!surface->IsOffscreen()) {
    if (CommandLine::ForCurrentProcess()->HasSwitch(switches::kDisableGpuVsync))
      context->SetSwapInterval(0);
    else
      context->SetSwapInterval(1);
  }
#endif

  // Do not limit to a certain number of commands before scheduling another
  // update when rendering onscreen.
  if (!surface->IsOffscreen())
    commands_per_update_ = INT_MAX;

  // Map the ring buffer and create the parser.
  Buffer ring_buffer = command_buffer_->GetRingBuffer();
  if (ring_buffer.ptr) {
    parser_.reset(new CommandParser(ring_buffer.ptr,
                                    ring_buffer.size,
                                    0,
                                    ring_buffer.size,
                                    0,
                                    decoder_.get()));
  } else {
    parser_.reset(new CommandParser(NULL, 0, 0, 0, 0,
                                    decoder_.get()));
  }

  // Initialize the decoder with either the view or pbuffer GLContext.
  // TODO(apatrick): The GpuScheduler should know nothing about the surface the
  // decoder is rendering to. Get rid of the surface parameter.
  if (!decoder_->Initialize(surface,
                            context,
                            size,
                            disallowed_extensions,
                            allowed_extensions,
                            attribs,
                            parent_decoder,
                            parent_texture_id)) {
    LOG(ERROR) << "GpuScheduler::InitializeCommon failed because decoder "
               << "failed to initialize.";
    Destroy();
    return false;
  }

  return true;
}

void GpuScheduler::DestroyCommon() {
  bool have_context = false;
  if (decoder_.get()) {
    have_context = decoder_->MakeCurrent();
    decoder_->Destroy();
    decoder_.reset();
  }

  parser_.reset();
}

#if defined(OS_MACOSX)
namespace {
const unsigned int kMaxOutstandingSwapBuffersCallsPerOnscreenContext = 1;
}
#endif

void GpuScheduler::PutChanged(bool sync) {
  TRACE_EVENT0("gpu", "GpuScheduler:PutChanged");
  CommandBuffer::State state = command_buffer_->GetState();
  parser_->set_put(state.put_offset);

  if (sync)
    ProcessCommands();
  else
    ScheduleProcessCommands();
}

void GpuScheduler::ProcessCommands() {
  TRACE_EVENT0("gpu", "GpuScheduler:ProcessCommands");
  CommandBuffer::State state = command_buffer_->GetState();
  if (state.error != error::kNoError)
    return;

  if (unscheduled_count_ > 0) {
    TRACE_EVENT1("gpu", "EarlyOut_Unscheduled",
                 "unscheduled_count_", unscheduled_count_);
    return;
  }

  if (decoder_.get()) {
    if (!decoder_->MakeCurrent()) {
      LOG(ERROR) << "Context lost because MakeCurrent failed.";
      command_buffer_->SetParseError(error::kLostContext);
      return;
    }
  }

#if defined(OS_MACOSX)
  bool do_rate_limiting = surface_.get() != NULL;
  // Don't swamp the browser process with SwapBuffers calls it can't handle.
  if (do_rate_limiting &&
      swap_buffers_count_ - acknowledged_swap_buffers_count_ >=
      kMaxOutstandingSwapBuffersCallsPerOnscreenContext) {
    // Stop doing work on this command buffer. In the GPU process,
    // receipt of the GpuMsg_AcceleratedSurfaceBuffersSwappedACK
    // message causes ProcessCommands to be scheduled again.
    return;
  }
#endif

  base::TimeTicks start_time = base::TimeTicks::Now();
  base::TimeDelta elapsed;
  bool is_break = false;
  error::Error error = error::kNoError;
  do {
    int commands_processed = 0;
    while (commands_processed < commands_per_update_ &&
           !parser_->IsEmpty()) {
      error = parser_->ProcessCommand();

      // TODO(piman): various classes duplicate various pieces of state, leading
      // to needlessly complex update logic. It should be possible to simply
      // share the state across all of them.
      command_buffer_->SetGetOffset(static_cast<int32>(parser_->get()));

      if (error == error::kWaiting || error == error::kYield) {
        is_break = true;
        break;
      } else if (error::IsError(error)) {
        command_buffer_->SetParseError(error);
        return;
      }

      if (unscheduled_count_ > 0) {
        is_break = true;
        break;
      }

      ++commands_processed;
      if (command_processed_callback_.get()) {
        command_processed_callback_->Run();
      }
    }
    elapsed = base::TimeTicks::Now() - start_time;
  } while(!is_break &&
          !parser_->IsEmpty() &&
          elapsed.InMicroseconds() < kMinimumSchedulerQuantumMicros);

  if (unscheduled_count_ == 0 &&
      error != error::kWaiting &&
      !parser_->IsEmpty()) {
    ScheduleProcessCommands();
  }
}

void GpuScheduler::SetScheduled(bool scheduled) {
  TRACE_EVENT2("gpu", "GpuScheduler:SetScheduled", "scheduled", scheduled,
               "unscheduled_count_", unscheduled_count_);
  if (scheduled) {
    --unscheduled_count_;
    DCHECK_GE(unscheduled_count_, 0);

    if (unscheduled_count_ == 0) {
      if (scheduled_callback_.get())
        scheduled_callback_->Run();

      ScheduleProcessCommands();
    }
  } else {
    ++unscheduled_count_;
  }
}

bool GpuScheduler::IsScheduled() {
  return unscheduled_count_ == 0;
}

void GpuScheduler::SetScheduledCallback(Callback0::Type* scheduled_callback) {
  scheduled_callback_.reset(scheduled_callback);
}

Buffer GpuScheduler::GetSharedMemoryBuffer(int32 shm_id) {
  return command_buffer_->GetTransferBuffer(shm_id);
}

void GpuScheduler::set_token(int32 token) {
  command_buffer_->SetToken(token);
}

bool GpuScheduler::SetGetOffset(int32 offset) {
  if (parser_->set_get(offset)) {
    command_buffer_->SetGetOffset(static_cast<int32>(parser_->get()));
    return true;
  }
  return false;
}

int32 GpuScheduler::GetGetOffset() {
  return parser_->get();
}

void GpuScheduler::ResizeOffscreenFrameBuffer(const gfx::Size& size) {
  decoder_->ResizeOffscreenFrameBuffer(size);
}

void GpuScheduler::SetResizeCallback(Callback1<gfx::Size>::Type* callback) {
  wrapped_resize_callback_.reset(callback);
  decoder_->SetResizeCallback(
      NewCallback(this,
                  &GpuScheduler::WillResize));
}

void GpuScheduler::SetSwapBuffersCallback(
    Callback0::Type* callback) {
  wrapped_swap_buffers_callback_.reset(callback);
  decoder_->SetSwapBuffersCallback(
      NewCallback(this,
                  &GpuScheduler::WillSwapBuffers));
}

void GpuScheduler::SetCommandProcessedCallback(
    Callback0::Type* callback) {
  command_processed_callback_.reset(callback);
}

void GpuScheduler::ScheduleProcessCommands() {
  MessageLoop::current()->PostTask(
      FROM_HERE,
      method_factory_.NewRunnableMethod(&GpuScheduler::ProcessCommands));
}

void GpuScheduler::WillResize(gfx::Size size) {
  if (wrapped_resize_callback_.get()) {
    wrapped_resize_callback_->Run(size);
  }
}

}  // namespace gpu