summaryrefslogtreecommitdiffstats
path: root/ui/surface/accelerated_surface_win.cc
blob: bb19b7d69f7d8f06fbbaa6fb406dca0ca30d55db (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
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
// Copyright (c) 2012 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 "ui/surface/accelerated_surface_win.h"

#include <windows.h>
#include <algorithm>

#include "base/bind.h"
#include "base/bind_helpers.h"
#include "base/callback.h"
#include "base/command_line.h"
#include "base/debug/trace_event.h"
#include "base/files/file_path.h"
#include "base/lazy_instance.h"
#include "base/memory/scoped_ptr.h"
#include "base/message_loop/message_loop_proxy.h"
#include "base/scoped_native_library.h"
#include "base/stringprintf.h"
#include "base/synchronization/waitable_event.h"
#include "base/threading/thread.h"
#include "base/threading/thread_restrictions.h"
#include "base/win/wrapped_window_proc.h"
#include "media/base/video_frame.h"
#include "media/base/video_util.h"
#include "third_party/skia/include/core/SkBitmap.h"
#include "ui/base/win/dpi.h"
#include "ui/base/win/hwnd_util.h"
#include "ui/base/win/shell.h"
#include "ui/gfx/rect.h"
#include "ui/gl/gl_switches.h"
#include "ui/surface/accelerated_surface_transformer_win.h"
#include "ui/surface/d3d9_utils_win.h"
#include "ui/surface/surface_switches.h"

namespace d3d_utils = ui_surface_d3d9_utils;

namespace {

UINT GetPresentationInterval() {
  if (CommandLine::ForCurrentProcess()->HasSwitch(switches::kDisableGpuVsync))
    return D3DPRESENT_INTERVAL_IMMEDIATE;
  else
    return D3DPRESENT_INTERVAL_ONE;
}

bool DoFirstShowPresentWithGDI() {
  return CommandLine::ForCurrentProcess()->HasSwitch(
      switches::kDoFirstShowPresentWithGDI);
}

bool DoAllShowPresentWithGDI() {
  return CommandLine::ForCurrentProcess()->HasSwitch(
      switches::kDoAllShowPresentWithGDI);
}

// Use a SurfaceReader to copy into one plane of the VideoFrame.
bool CopyPlane(AcceleratedSurfaceTransformer* gpu_ops,
               IDirect3DSurface9* src_surface,
               media::VideoFrame* dst_frame,
               size_t plane_id) {
  int width_in_bytes = dst_frame->row_bytes(plane_id);
  return gpu_ops->ReadFast(src_surface, dst_frame->data(plane_id),
                           width_in_bytes, dst_frame->rows(plane_id),
                           dst_frame->row_bytes(plane_id));
}

}  // namespace

// A PresentThread is a thread that is dedicated to presenting surfaces to a
// window. It owns a Direct3D device and a Direct3D query for this purpose.
class PresentThread : public base::Thread,
                      public base::RefCountedThreadSafe<PresentThread> {
 public:
  PresentThread(const char* name, uint64 adapter_luid);

  IDirect3DDevice9Ex* device() { return device_.get(); }
  IDirect3DQuery9* query() { return query_.get(); }
  AcceleratedSurfaceTransformer* surface_transformer() {
    return &surface_transformer_;
  }

  void SetAdapterLUID(uint64 adapter_luid);
  void InitDevice();
  void LockAndResetDevice();
  void ResetDevice();
  bool IsDeviceLost();

  base::Lock* lock() {
    return &lock_;
  }

 protected:
  virtual void Init();
  virtual void CleanUp();

 private:
  friend class base::RefCountedThreadSafe<PresentThread>;

  ~PresentThread();

  // The lock is taken while any thread is calling an AcceleratedPresenter
  // associated with this thread.
  base::Lock lock_;

  base::ScopedNativeLibrary d3d_module_;
  uint64 adapter_luid_;
  base::win::ScopedComPtr<IDirect3DDevice9Ex> device_;

  // This query is used to wait until a certain amount of progress has been
  // made by the GPU and it is safe for the producer to modify its shared
  // texture again.
  base::win::ScopedComPtr<IDirect3DQuery9> query_;
  AcceleratedSurfaceTransformer surface_transformer_;

  DISALLOW_COPY_AND_ASSIGN(PresentThread);
};

// There is a fixed sized pool of PresentThreads and therefore the maximum
// number of Direct3D devices owned by those threads is bounded.
class PresentThreadPool {
 public:
  static const int kNumPresentThreads = 4;

  PresentThreadPool();
  PresentThread* NextThread();

  void SetAdapterLUID(uint64 adapter_luid);

 private:
  base::Lock lock_;
  int next_thread_;
  scoped_refptr<PresentThread> present_threads_[kNumPresentThreads];
  uint64 adapter_luid_;

  DISALLOW_COPY_AND_ASSIGN(PresentThreadPool);
};

// A thread safe map of presenters by surface ID that returns presenters via
// a scoped_refptr to keep them alive while they are referenced.
class AcceleratedPresenterMap {
 public:
  AcceleratedPresenterMap();
  scoped_refptr<AcceleratedPresenter> CreatePresenter(
      gfx::PluginWindowHandle window);
  void RemovePresenter(const scoped_refptr<AcceleratedPresenter>& presenter);
  scoped_refptr<AcceleratedPresenter> GetPresenter(
      gfx::PluginWindowHandle window);

  // Destroy any D3D resources owned by the given present thread. Called on
  // the given present thread.
  void ResetPresentThread(PresentThread* present_thread);

 private:
  base::Lock lock_;
  typedef std::map<gfx::PluginWindowHandle, AcceleratedPresenter*> PresenterMap;
  PresenterMap presenters_;
  uint64 adapter_luid_;
  DISALLOW_COPY_AND_ASSIGN(AcceleratedPresenterMap);
};

base::LazyInstance<PresentThreadPool>
    g_present_thread_pool = LAZY_INSTANCE_INITIALIZER;

base::LazyInstance<AcceleratedPresenterMap>
    g_accelerated_presenter_map = LAZY_INSTANCE_INITIALIZER;

PresentThread::PresentThread(const char* name, uint64 adapter_luid)
    : base::Thread(name),
      adapter_luid_(adapter_luid) {
}

void PresentThread::SetAdapterLUID(uint64 adapter_luid) {
  base::AutoLock locked(lock_);

  CHECK(message_loop() == MessageLoop::current());

  if (adapter_luid_ == adapter_luid)
    return;

  adapter_luid_ = adapter_luid;
  if (device_)
    ResetDevice();
}

void PresentThread::InitDevice() {
  lock_.AssertAcquired();

  if (device_)
    return;

  TRACE_EVENT0("gpu", "PresentThread::Init");
  d3d_utils::LoadD3D9(&d3d_module_);
  ResetDevice();
}

void PresentThread::LockAndResetDevice() {
  base::AutoLock locked(lock_);
  ResetDevice();
}

void PresentThread::ResetDevice() {
  TRACE_EVENT0("gpu", "PresentThread::ResetDevice");

  lock_.AssertAcquired();

  // The D3D device must be created on the present thread.
  CHECK(message_loop() == base::MessageLoop::current());

  // This will crash some Intel drivers but we can't render anything without
  // reseting the device, which would be disappointing.
  query_ = NULL;
  device_ = NULL;
  surface_transformer_.ReleaseAll();

  g_accelerated_presenter_map.Pointer()->ResetPresentThread(this);

  if (!d3d_utils::CreateDevice(d3d_module_,
                               adapter_luid_,
                               D3DDEVTYPE_HAL,
                               GetPresentationInterval(),
                               device_.Receive())) {
    return;
  }

  HRESULT hr = device_->CreateQuery(D3DQUERYTYPE_EVENT, query_.Receive());
  if (FAILED(hr)) {
    LOG(ERROR) << "Failed to create query";
    device_ = NULL;
    return;
  }

  if (!surface_transformer_.Init(device_)) {
    LOG(ERROR) << "Failed to initialize surface transformer";
    query_ = NULL;
    device_ = NULL;
    return;
  }
}

bool PresentThread::IsDeviceLost() {
  lock_.AssertAcquired();

  HRESULT hr = device_->CheckDeviceState(NULL);
  return FAILED(hr) || hr == S_PRESENT_MODE_CHANGED;
}

void PresentThread::Init() {
  TRACE_EVENT0("gpu", "Initialize thread");
}

void PresentThread::CleanUp() {
  // The D3D device and query are leaked because destroying the associated D3D
  // query crashes some Intel drivers.
  surface_transformer_.DetachAll();
  device_.Detach();
  query_.Detach();
}

PresentThread::~PresentThread() {
  Stop();
}

PresentThreadPool::PresentThreadPool() : next_thread_(0) {
}

PresentThread* PresentThreadPool::NextThread() {
  base::AutoLock locked(lock_);

  next_thread_ = (next_thread_ + 1) % kNumPresentThreads;
  PresentThread* thread = present_threads_[next_thread_].get();
  if (!thread) {
    thread = new PresentThread(
        base::StringPrintf("PresentThread #%d", next_thread_).c_str(),
        adapter_luid_);
    thread->Start();
    present_threads_[next_thread_] = thread;
  }

  return thread;
}

void PresentThreadPool::SetAdapterLUID(uint64 adapter_luid) {
  base::AutoLock locked(lock_);

  adapter_luid_ = adapter_luid;

  for (int i = 0; i < kNumPresentThreads; ++i) {
    if (!present_threads_[i])
      continue;

    present_threads_[i]->message_loop()->PostTask(
        FROM_HERE,
        base::Bind(&PresentThread::SetAdapterLUID,
                   present_threads_[i],
                   adapter_luid));
  }
}

AcceleratedPresenterMap::AcceleratedPresenterMap() {
}

scoped_refptr<AcceleratedPresenter> AcceleratedPresenterMap::CreatePresenter(
    gfx::PluginWindowHandle window) {
  scoped_refptr<AcceleratedPresenter> presenter(
      new AcceleratedPresenter(window));

  base::AutoLock locked(lock_);
  DCHECK(presenters_.find(window) == presenters_.end());
  presenters_[window] = presenter.get();

  return presenter;
}

void AcceleratedPresenterMap::RemovePresenter(
    const scoped_refptr<AcceleratedPresenter>& presenter) {
  base::AutoLock locked(lock_);
  for (PresenterMap::iterator it = presenters_.begin();
      it != presenters_.end();
      ++it) {
    if (it->second == presenter.get()) {
      presenters_.erase(it);
      return;
    }
  }

  NOTREACHED();
}

scoped_refptr<AcceleratedPresenter> AcceleratedPresenterMap::GetPresenter(
    gfx::PluginWindowHandle window) {
  base::AutoLock locked(lock_);

#if defined(USE_AURA)
  if (!window)
    return presenters_.begin()->second;
#endif

  PresenterMap::iterator it = presenters_.find(window);
  if (it == presenters_.end())
    return scoped_refptr<AcceleratedPresenter>();

  return it->second;
}

void AcceleratedPresenterMap::ResetPresentThread(
    PresentThread* present_thread) {
  base::AutoLock locked(lock_);

  for (PresenterMap::iterator it = presenters_.begin();
      it != presenters_.end();
      ++it) {
    it->second->ResetPresentThread(present_thread);
  }
}

AcceleratedPresenter::AcceleratedPresenter(gfx::PluginWindowHandle window)
    : present_thread_(g_present_thread_pool.Pointer()->NextThread()),
      window_(window),
      event_(false, false),
      hidden_(true),
      do_present_with_GDI_(DoAllShowPresentWithGDI() ||
                           DoFirstShowPresentWithGDI()),
      is_session_locked_(false) {
}

// static
void AcceleratedPresenter::SetAdapterLUID(uint64 adapter_luid) {
  return g_present_thread_pool.Pointer()->SetAdapterLUID(adapter_luid);
}


// static
scoped_refptr<AcceleratedPresenter> AcceleratedPresenter::GetForWindow(
    gfx::PluginWindowHandle window) {
  return g_accelerated_presenter_map.Pointer()->GetPresenter(window);
}

void AcceleratedPresenter::AsyncPresentAndAcknowledge(
    const gfx::Size& size,
    int64 surface_handle,
    const CompletionTask& completion_task) {
  if (!surface_handle) {
    TRACE_EVENT1("gpu", "EarlyOut_ZeroSurfaceHandle",
                 "surface_handle", surface_handle);
    completion_task.Run(true, base::TimeTicks(), base::TimeDelta());
    return;
  }

  present_thread_->message_loop()->PostTask(
      FROM_HERE,
      base::Bind(&AcceleratedPresenter::DoPresentAndAcknowledge,
                 this,
                 size,
                 surface_handle,
                 completion_task));
}

void AcceleratedPresenter::Present(HDC dc) {
  TRACE_EVENT0("gpu", "Present");

  base::AutoLock locked(*present_thread_->lock());

  // If invalidated, do nothing. The window is gone.
  if (!window_)
    return;

  // Suspended or nothing has ever been presented.
  if (!swap_chain_)
    return;

  PresentWithGDI(dc);
}

void AcceleratedPresenter::AsyncCopyTo(
    const gfx::Rect& requested_src_subrect,
    const gfx::Size& dst_size,
    const base::Callback<void(bool, const SkBitmap&)>& callback) {
  present_thread_->message_loop()->PostTask(
      FROM_HERE,
      base::Bind(&AcceleratedPresenter::DoCopyToAndAcknowledge,
                 this,
                 requested_src_subrect,
                 dst_size,
                 base::MessageLoopProxy::current(),
                 callback));
}

void AcceleratedPresenter::AsyncCopyToVideoFrame(
    const gfx::Rect& requested_src_subrect,
    const scoped_refptr<media::VideoFrame>& target,
    const base::Callback<void(bool)>& callback) {
  present_thread_->message_loop()->PostTask(
      FROM_HERE,
      base::Bind(&AcceleratedPresenter::DoCopyToVideoFrameAndAcknowledge,
                 this,
                 requested_src_subrect,
                 target,
                 base::MessageLoopProxy::current(),
                 callback));
}

void AcceleratedPresenter::DoCopyToAndAcknowledge(
    const gfx::Rect& src_subrect,
    const gfx::Size& dst_size,
    scoped_refptr<base::SingleThreadTaskRunner> callback_runner,
    const base::Callback<void(bool, const SkBitmap&)>& callback) {
  SkBitmap target;
  bool result = DoCopyToARGB(src_subrect, dst_size, &target);
  if (!result)
    target.reset();
  callback_runner->PostTask(FROM_HERE, base::Bind(callback, result, target));
}

void AcceleratedPresenter::DoCopyToVideoFrameAndAcknowledge(
    const gfx::Rect& src_subrect,
    const scoped_refptr<media::VideoFrame>& target,
    const scoped_refptr<base::SingleThreadTaskRunner>& callback_runner,
    const base::Callback<void(bool)>& callback) {

  bool result = DoCopyToYUV(src_subrect, target);
  callback_runner->PostTask(FROM_HERE, base::Bind(callback, result));
}

bool AcceleratedPresenter::DoCopyToARGB(const gfx::Rect& requested_src_subrect,
                                        const gfx::Size& dst_size,
                                        SkBitmap* bitmap) {
  TRACE_EVENT2(
      "gpu", "CopyTo",
      "width", dst_size.width(),
      "height", dst_size.height());

  base::AutoLock locked(*present_thread_->lock());

  if (!swap_chain_)
    return false;

  AcceleratedSurfaceTransformer* gpu_ops =
      present_thread_->surface_transformer();

  base::win::ScopedComPtr<IDirect3DSurface9> back_buffer;
  HRESULT hr = swap_chain_->GetBackBuffer(0,
                                          D3DBACKBUFFER_TYPE_MONO,
                                          back_buffer.Receive());
  if (FAILED(hr)) {
    LOG(ERROR) << "Failed to get back buffer";
    return false;
  }

  D3DSURFACE_DESC desc;
  hr = back_buffer->GetDesc(&desc);
  if (FAILED(hr)) {
    LOG(ERROR) << "Failed to get buffer description";
    return false;
  }

  const gfx::Size back_buffer_size(desc.Width, desc.Height);
  if (back_buffer_size.IsEmpty())
    return false;

  // With window resizing, it's possible that the back buffer is smaller than
  // the requested src subset. Clip to the actual back buffer.
  gfx::Rect src_subrect = requested_src_subrect;
  src_subrect.Intersect(gfx::Rect(back_buffer_size));
  base::win::ScopedComPtr<IDirect3DSurface9> final_surface;
  {
    if (!d3d_utils::CreateOrReuseLockableSurface(present_thread_->device(),
                                                 dst_size,
                                                 &final_surface)) {
      LOG(ERROR) << "Failed to create temporary lockable surface";
      return false;
    }
  }

  {
    // Let the surface transformer start the resize into |final_surface|.
    TRACE_EVENT0("gpu", "ResizeBilinear");
    if (!gpu_ops->ResizeBilinear(back_buffer, src_subrect,
                                 final_surface, gfx::Rect(dst_size))) {
      LOG(ERROR) << "Failed to resize bilinear";
      return false;
    }
  }

  bitmap->setConfig(SkBitmap::kARGB_8888_Config,
                    dst_size.width(), dst_size.height());
  if (!bitmap->allocPixels())
    return false;
  bitmap->setIsOpaque(true);

  // Copy |final_surface| to |bitmap|. This is always a synchronous operation.
  return gpu_ops->ReadFast(final_surface,
                           reinterpret_cast<uint8*>(bitmap->getPixels()),
                           bitmap->width() * bitmap->bytesPerPixel(),
                           bitmap->height(),
                           static_cast<int>(bitmap->rowBytes()));
}

bool AcceleratedPresenter::DoCopyToYUV(
    const gfx::Rect& requested_src_subrect,
    const scoped_refptr<media::VideoFrame>& frame) {
  gfx::Size dst_size = frame->coded_size();
  TRACE_EVENT2(
      "gpu", "CopyToYUV",
      "width", dst_size.width(),
      "height", dst_size.height());

  base::AutoLock locked(*present_thread_->lock());

  if (!swap_chain_)
    return false;

  AcceleratedSurfaceTransformer* gpu_ops =
      present_thread_->surface_transformer();

  base::win::ScopedComPtr<IDirect3DSurface9> back_buffer;
  HRESULT hr = swap_chain_->GetBackBuffer(0,
                                          D3DBACKBUFFER_TYPE_MONO,
                                          back_buffer.Receive());
  if (FAILED(hr))
    return false;

  D3DSURFACE_DESC desc;
  hr = back_buffer->GetDesc(&desc);
  if (FAILED(hr))
    return false;

  const gfx::Size back_buffer_size(desc.Width, desc.Height);
  if (back_buffer_size.IsEmpty())
    return false;

  // With window resizing, it's possible that the back buffer is smaller than
  // the requested src subset. Clip to the actual back buffer.
  gfx::Rect src_subrect = requested_src_subrect;
  src_subrect.Intersect(gfx::Rect(back_buffer_size));

  base::win::ScopedComPtr<IDirect3DSurface9> resized;
  base::win::ScopedComPtr<IDirect3DTexture9> resized_as_texture;
  if (!gpu_ops->GetIntermediateTexture(dst_size,
                                       resized_as_texture.Receive(),
                                       resized.Receive())) {
    return false;
  }

  // Shrink the source to fit entirely in the destination while preserving
  // aspect ratio. Fill in any margin with black.
  // TODO(nick): It would be more efficient all around to implement
  // letterboxing as a memset() on the dst.
  gfx::Rect letterbox = media::ComputeLetterboxRegion(gfx::Rect(dst_size),
                                                      src_subrect.size());
  if (letterbox != gfx::Rect(dst_size)) {
    TRACE_EVENT0("gpu", "Letterbox");
    present_thread_->device()->ColorFill(resized, NULL, 0xFF000000);
  }

  {
    TRACE_EVENT0("gpu", "ResizeBilinear");
    if (!gpu_ops->ResizeBilinear(back_buffer, src_subrect, resized, letterbox))
      return false;
  }

  base::win::ScopedComPtr<IDirect3DSurface9> y, u, v;
  {
    TRACE_EVENT0("gpu", "TransformRGBToYV12");
    if (!gpu_ops->TransformRGBToYV12(resized_as_texture,
                                     dst_size,
                                     y.Receive(), u.Receive(), v.Receive())) {
      return false;
    }
  }

  if (!CopyPlane(gpu_ops, y, frame, media::VideoFrame::kYPlane))
    return false;
  if (!CopyPlane(gpu_ops, u, frame, media::VideoFrame::kUPlane))
    return false;
  if (!CopyPlane(gpu_ops, v, frame, media::VideoFrame::kVPlane))
    return false;
  return true;
}

void AcceleratedPresenter::Suspend() {
  present_thread_->message_loop()->PostTask(
      FROM_HERE,
      base::Bind(&AcceleratedPresenter::DoSuspend,
                 this));
}

void AcceleratedPresenter::WasHidden() {
  base::AutoLock locked(*present_thread_->lock());
  hidden_ = true;
}

void AcceleratedPresenter::ReleaseSurface() {
  present_thread_->message_loop()->PostTask(
      FROM_HERE,
      base::Bind(&AcceleratedPresenter::DoReleaseSurface,
                 this));
}

void AcceleratedPresenter::SetIsSessionLocked(bool locked) {
  is_session_locked_ = locked;
}

void AcceleratedPresenter::Invalidate() {
  // Make any pending or future presentation tasks do nothing. Once the last
  // last pending task has been ignored, the reference count on the presenter
  // will go to zero and the presenter, and potentially also the present thread
  // it has a reference count on, will be destroyed.
  base::AutoLock locked(*present_thread_->lock());
  window_ = NULL;
}

void AcceleratedPresenter::ResetPresentThread(
    PresentThread* present_thread) {
  TRACE_EVENT0("gpu", "ResetPresentThread");

  // present_thread_ can be accessed without the lock because it is immutable.
  if (present_thread_ != present_thread)
    return;

  present_thread_->lock()->AssertAcquired();

  source_texture_ = NULL;
  swap_chain_ = NULL;
  quantized_size_ = gfx::Size();
}

#if defined(USE_AURA)
void AcceleratedPresenter::SetNewTargetWindow(gfx::PluginWindowHandle window) {
  window_ = window;
  swap_chain_ = NULL;
}
#endif

AcceleratedPresenter::~AcceleratedPresenter() {
}

void AcceleratedPresenter::DoPresentAndAcknowledge(
    const gfx::Size& size,
    int64 surface_handle,
    const CompletionTask& completion_task) {
  TRACE_EVENT2(
      "gpu", "DoPresentAndAcknowledge",
      "width", size.width(),
      "height", size.height());

  HRESULT hr;

  base::AutoLock locked(*present_thread_->lock());

  // Initialize the device lazily since calling Direct3D can crash bots.
  present_thread_->InitDevice();

  if (!present_thread_->device()) {
    completion_task.Run(false, base::TimeTicks(), base::TimeDelta());
    TRACE_EVENT0("gpu", "EarlyOut_NoDevice");
    return;
  }

  // Ensure the task is acknowledged on early out after this point.
  base::ScopedClosureRunner scoped_completion_runner(
      base::Bind(completion_task, true, base::TimeTicks(), base::TimeDelta()));

  // If invalidated, do nothing, the window is gone.
  if (!window_) {
    TRACE_EVENT0("gpu", "EarlyOut_NoWindow");
    return;
  }

#if !defined(USE_AURA)
  // If the window is a different size than the swap chain that is being
  // presented then drop the frame.
  gfx::Size window_size = GetWindowSize();
#if defined(ENABLE_HIDPI)
  // Check if the size mismatch is within allowable round off or truncation
  // error.
  gfx::Size dip_size = ui::win::ScreenToDIPSize(window_size);
  gfx::Size pixel_size = ui::win::DIPToScreenSize(dip_size);
  bool size_mismatch = abs(window_size.width() - size.width()) >
      abs(window_size.width() - pixel_size.width()) ||
      abs(window_size.height() - size.height()) >
      abs(window_size.height() - pixel_size.height());
#else
  bool size_mismatch = size != window_size;
#endif
  if (hidden_ && size_mismatch) {
    TRACE_EVENT2("gpu", "EarlyOut_WrongWindowSize",
                 "backwidth", size.width(), "backheight", size.height());
    TRACE_EVENT2("gpu", "EarlyOut_WrongWindowSize2",
                 "windowwidth", window_size.width(),
                 "windowheight", window_size.height());
    return;
  }
#endif

  // Round up size so the swap chain is not continuously resized with the
  // surface, which could lead to memory fragmentation.
  const int kRound = 64;
  gfx::Size quantized_size(
      std::max(1, (size.width() + kRound - 1) / kRound * kRound),
      std::max(1, (size.height() + kRound - 1) / kRound * kRound));

  // Ensure the swap chain exists and is the same size (rounded up) as the
  // surface to be presented.
  if (!swap_chain_ || quantized_size_ != quantized_size) {
    TRACE_EVENT0("gpu", "CreateAdditionalSwapChain");
    quantized_size_ = quantized_size;

    D3DPRESENT_PARAMETERS parameters = { 0 };
    parameters.BackBufferWidth = quantized_size.width();
    parameters.BackBufferHeight = quantized_size.height();
    parameters.BackBufferCount = 1;
    parameters.BackBufferFormat = D3DFMT_A8R8G8B8;
    parameters.hDeviceWindow = window_;
    parameters.Windowed = TRUE;
    parameters.Flags = 0;
    parameters.PresentationInterval = GetPresentationInterval();
    parameters.SwapEffect = D3DSWAPEFFECT_COPY;

    swap_chain_ = NULL;
    HRESULT hr = present_thread_->device()->CreateAdditionalSwapChain(
        &parameters,
        swap_chain_.Receive());
    if (FAILED(hr)) {
      LOG(ERROR) << "Failed to create swap chain "
                 << quantized_size.width() << " x " <<quantized_size.height();
      return;
    }
  }

  if (!source_texture_.get()) {
    TRACE_EVENT0("gpu", "OpenSharedTexture");
    if (!d3d_utils::OpenSharedTexture(present_thread_->device(),
                                      surface_handle,
                                      size,
                                      source_texture_.Receive())) {
      LOG(ERROR) << "Failed to open shared texture";
      return;
    }
  }

  base::win::ScopedComPtr<IDirect3DSurface9> source_surface;
  hr = source_texture_->GetSurfaceLevel(0, source_surface.Receive());
  if (FAILED(hr)) {
    TRACE_EVENT0("gpu", "EarlyOut_NoSurfaceLevel");
    LOG(ERROR) << "Failed to get source surface";
    return;
  }

  base::win::ScopedComPtr<IDirect3DSurface9> dest_surface;
  hr = swap_chain_->GetBackBuffer(0,
                                  D3DBACKBUFFER_TYPE_MONO,
                                  dest_surface.Receive());
  if (FAILED(hr)) {
    TRACE_EVENT0("gpu", "EarlyOut_NoBackbuffer");
    LOG(ERROR) << "Failed to get back buffer";
    return;
  }

  RECT rect = {
    0, 0,
    size.width(), size.height()
  };

  {
    TRACE_EVENT0("gpu", "Copy");

    // Copy while flipping the source texture on the vertical axis.
    bool result = present_thread_->surface_transformer()->CopyInverted(
        source_texture_, dest_surface, size);
    if (!result) {
      LOG(ERROR) << "Failed to copy shared texture";
      return;
    }
  }

  hr = present_thread_->query()->Issue(D3DISSUE_END);
  if (FAILED(hr)) {
    LOG(ERROR) << "Failed to issue query";
    return;
  }

  present_size_ = size;

  // If it is expected that Direct3D cannot be used reliably because the window
  // is resizing, fall back to presenting with GDI.
  if (CheckDirect3DWillWork()) {
    TRACE_EVENT0("gpu", "PresentD3D");

    hr = swap_chain_->Present(&rect, &rect, window_, NULL, 0);

    // For latency_tests.cc:
    UNSHIPPED_TRACE_EVENT_INSTANT0("test_gpu", "CompositorSwapBuffersComplete",
                                   TRACE_EVENT_SCOPE_THREAD);

    if (FAILED(hr)) {
      if (present_thread_->IsDeviceLost())
        present_thread_->ResetDevice();
      return;
    }
  } else {
    HDC dc = GetDC(window_);
    PresentWithGDI(dc);
    ReleaseDC(window_, dc);
  }

  hidden_ = false;

  D3DDISPLAYMODE display_mode;
  hr = present_thread_->device()->GetDisplayMode(0, &display_mode);
  if (FAILED(hr)) {
    LOG(ERROR) << "Failed to get display mode";
    return;
  }

  D3DRASTER_STATUS raster_status;
  hr = swap_chain_->GetRasterStatus(&raster_status);
  if (FAILED(hr)) {
    LOG(ERROR) << "Failed to get raster status";
    return;
  }

  // I can't figure out how to determine how many scanlines are in the
  // vertical blank so clamp it such that scanline / height <= 1.
  int clamped_scanline = std::min(raster_status.ScanLine, display_mode.Height);

  // The Internet says that on some GPUs, the scanline is not available
  // while in the vertical blank.
  if (raster_status.InVBlank)
    clamped_scanline = display_mode.Height;

  base::TimeTicks current_time = base::TimeTicks::HighResNow();

  // Figure out approximately how far back in time the last vsync was based on
  // the ratio of the raster scanline to the display height.
  base::TimeTicks last_vsync_time;
  base::TimeDelta refresh_period;
  if (display_mode.Height) {
      last_vsync_time = current_time -
        base::TimeDelta::FromMilliseconds((clamped_scanline * 1000) /
            (display_mode.RefreshRate * display_mode.Height));
      refresh_period = base::TimeDelta::FromMicroseconds(
          1000000 / display_mode.RefreshRate);
  }

  // Wait for the StretchRect to complete before notifying the GPU process
  // that it is safe to write to its backing store again.
  {
    TRACE_EVENT0("gpu", "spin");

    do {
      hr = present_thread_->query()->GetData(NULL, 0, D3DGETDATA_FLUSH);
      if (hr == S_FALSE) {
        Sleep(1);

        if (present_thread_->IsDeviceLost()) {
          present_thread_->ResetDevice();
          return;
        }
      }
    } while (hr == S_FALSE);
  }

  scoped_completion_runner.Release();
  completion_task.Run(true, last_vsync_time, refresh_period);
}

void AcceleratedPresenter::DoSuspend() {
  base::AutoLock locked(*present_thread_->lock());
  swap_chain_ = NULL;
}

void AcceleratedPresenter::DoReleaseSurface() {
  base::AutoLock locked(*present_thread_->lock());
  present_thread_->InitDevice();
  source_texture_.Release();
}

void AcceleratedPresenter::PresentWithGDI(HDC dc) {
  TRACE_EVENT0("gpu", "PresentWithGDI");

  if (!present_thread_->device()) {
    LOG(ERROR) << "No device";
    return;
  }

  if (!swap_chain_) {
    LOG(ERROR) << "No swap chain";
    return;
  }

  base::win::ScopedComPtr<IDirect3DTexture9> system_texture;
  {
    TRACE_EVENT0("gpu", "CreateSystemTexture");
    HRESULT hr = present_thread_->device()->CreateTexture(
        quantized_size_.width(),
        quantized_size_.height(),
        1,
        0,
        D3DFMT_A8R8G8B8,
        D3DPOOL_SYSTEMMEM,
        system_texture.Receive(),
        NULL);
    if (FAILED(hr)) {
      LOG(ERROR) << "Failed to create system memory texture";
      return;
    }
  }

  base::win::ScopedComPtr<IDirect3DSurface9> system_surface;
  HRESULT hr = system_texture->GetSurfaceLevel(0, system_surface.Receive());
  DCHECK(SUCCEEDED(hr));

  base::win::ScopedComPtr<IDirect3DSurface9> back_buffer;
  hr = swap_chain_->GetBackBuffer(0,
                                  D3DBACKBUFFER_TYPE_MONO,
                                  back_buffer.Receive());
  DCHECK(SUCCEEDED(hr));

  {
    TRACE_EVENT0("gpu", "GetRenderTargetData");
    hr = present_thread_->device()->GetRenderTargetData(back_buffer,
                                                        system_surface);

    if (FAILED(hr)) {
      if (present_thread_->IsDeviceLost()) {
        present_thread_->message_loop()->PostTask(
            FROM_HERE,
            base::Bind(&PresentThread::LockAndResetDevice, present_thread_));
      }
      return;
    }

    DCHECK(SUCCEEDED(hr));
  }

  D3DLOCKED_RECT locked_surface;
  hr = system_surface->LockRect(&locked_surface, NULL, D3DLOCK_READONLY);
  DCHECK(SUCCEEDED(hr));

  BITMAPINFO bitmap_info = {
    {
      sizeof(BITMAPINFOHEADER),
      quantized_size_.width(),
      -quantized_size_.height(),
      1,  // planes
      32,  // bitcount
      BI_RGB
    },
    {
      {0, 0, 0, 0}
    }
  };

  {
    TRACE_EVENT0("gpu", "StretchDIBits");
    StretchDIBits(dc,
                  0, 0,
                  present_size_.width(),
                  present_size_.height(),
                  0, 0,
                  present_size_.width(),
                  present_size_.height(),
                  locked_surface.pBits,
                  &bitmap_info,
                  DIB_RGB_COLORS,
                  SRCCOPY);
  }

  system_surface->UnlockRect();

  // For latency_tests.cc:
  UNSHIPPED_TRACE_EVENT_INSTANT0("test_gpu", "CompositorSwapBuffersComplete",
                                 TRACE_EVENT_SCOPE_THREAD);
}

gfx::Size AcceleratedPresenter::GetWindowSize() {
  RECT rect;
  GetClientRect(window_, &rect);
  return gfx::Rect(rect).size();
}

bool AcceleratedPresenter::CheckDirect3DWillWork() {
  // On a composited desktop, when the screen saver or logon screen are
  // active, D3D presents never make it to the window but GDI presents
  // do. If the session is locked GDI presents can be avoided since
  // the window gets a message on unlock and forces a repaint.
  if (!is_session_locked_ && ui::win::IsAeroGlassEnabled()) {
    // Failure to open the input desktop is a sign of running with a non-default
    // desktop.
    HDESK input_desktop = ::OpenInputDesktop(0, 0, GENERIC_READ);
    if (!input_desktop)
      return false;
    ::CloseDesktop(input_desktop);
  }

  gfx::Size window_size = GetWindowSize();
  if (window_size != last_window_size_ && last_window_size_.GetArea() != 0) {
    last_window_size_ = window_size;
    last_window_resize_time_ = base::Time::Now();
    return false;
  }

  if (do_present_with_GDI_ && hidden_) {
    if (DoFirstShowPresentWithGDI())
      do_present_with_GDI_ = false;

    return false;
  }

  return base::Time::Now() - last_window_resize_time_ >
      base::TimeDelta::FromMilliseconds(100);
}

AcceleratedSurface::AcceleratedSurface(gfx::PluginWindowHandle window)
    : presenter_(g_accelerated_presenter_map.Pointer()->CreatePresenter(
          window)) {
}

AcceleratedSurface::~AcceleratedSurface() {
  g_accelerated_presenter_map.Pointer()->RemovePresenter(presenter_);
  presenter_->Invalidate();
}

void AcceleratedSurface::Present(HDC dc) {
  presenter_->Present(dc);
}

void AcceleratedSurface::AsyncCopyTo(
    const gfx::Rect& src_subrect,
    const gfx::Size& dst_size,
    const base::Callback<void(bool, const SkBitmap&)>& callback) {
  presenter_->AsyncCopyTo(src_subrect, dst_size, callback);
}

void AcceleratedSurface::AsyncCopyToVideoFrame(
    const gfx::Rect& src_subrect,
    const scoped_refptr<media::VideoFrame>& target,
    const base::Callback<void(bool)>& callback) {
  presenter_->AsyncCopyToVideoFrame(src_subrect, target, callback);
}

void AcceleratedSurface::Suspend() {
  presenter_->Suspend();
}

void AcceleratedSurface::WasHidden() {
  presenter_->WasHidden();
}

void AcceleratedSurface::SetIsSessionLocked(bool locked) {
  presenter_->SetIsSessionLocked(locked);
}