Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright 2014 The Chromium Authors. All rights reserved. | 1 // Copyright 2014 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "content/browser/compositor/software_output_device_win.h" | 5 #include "content/browser/compositor/software_output_device_win.h" |
| 6 | 6 |
| 7 #include "base/memory/shared_memory.h" | |
| 8 #include "base/memory/singleton.h" | |
| 7 #include "content/public/browser/browser_thread.h" | 9 #include "content/public/browser/browser_thread.h" |
| 8 #include "third_party/skia/include/core/SkBitmap.h" | 10 #include "third_party/skia/include/core/SkBitmap.h" |
| 9 #include "third_party/skia/include/core/SkDevice.h" | 11 #include "third_party/skia/include/core/SkDevice.h" |
| 10 #include "ui/compositor/compositor.h" | 12 #include "ui/compositor/compositor.h" |
| 11 #include "ui/gfx/canvas.h" | 13 #include "ui/gfx/canvas.h" |
| 12 #include "ui/gfx/canvas_skia_paint.h" | 14 #include "ui/gfx/canvas_skia_paint.h" |
| 13 #include "ui/gfx/gdi_util.h" | 15 #include "ui/gfx/gdi_util.h" |
| 14 #include "ui/gfx/skia_util.h" | 16 #include "ui/gfx/skia_util.h" |
| 15 | 17 |
| 16 namespace content { | 18 namespace content { |
| 17 | 19 |
| 20 namespace { | |
| 21 | |
| 22 class SharedMemoryBacking { | |
| 23 public: | |
| 24 static SharedMemoryBacking* GetInstance() { | |
|
piman
2015/05/11 23:20:56
nit: could the SharedMemoryBacking be owned by the
| |
| 25 return Singleton<SharedMemoryBacking>::get(); | |
| 26 } | |
| 27 | |
| 28 void Resized() { | |
| 29 size_t new_size = GetMaxByteSize(); | |
| 30 if (new_size == created_byte_size_) | |
| 31 return; | |
| 32 for (SoftwareOutputDeviceWin* device : devices_) { | |
| 33 device->ReleaseContents(); | |
| 34 } | |
| 35 backing_.reset(); | |
| 36 created_byte_size_ = 0; | |
| 37 } | |
| 38 | |
| 39 void RegisterOutputDevice(SoftwareOutputDeviceWin* device) { | |
| 40 devices_.push_back(device); | |
| 41 } | |
| 42 | |
| 43 void UnregisterOutputDevice(SoftwareOutputDeviceWin* device) { | |
| 44 auto it = std::find(devices_.begin(), devices_.end(), device); | |
| 45 DCHECK(it != devices_.end()); | |
| 46 devices_.erase(it); | |
| 47 Resized(); | |
| 48 } | |
| 49 | |
| 50 base::SharedMemory* GetSharedMemory() { | |
| 51 if (backing_) | |
| 52 return backing_.get(); | |
| 53 created_byte_size_ = GetMaxByteSize(); | |
| 54 | |
| 55 backing_.reset(new base::SharedMemory); | |
| 56 CHECK(backing_->CreateAnonymous(created_byte_size_)); | |
| 57 return backing_.get(); | |
| 58 } | |
| 59 | |
| 60 private: | |
| 61 SharedMemoryBacking() : created_byte_size_(0) {} | |
| 62 | |
| 63 ~SharedMemoryBacking() { | |
| 64 DCHECK(devices_.empty()); | |
| 65 } | |
| 66 | |
| 67 size_t GetMaxByteSize() { | |
| 68 size_t max_size = 0; | |
| 69 for (const SoftwareOutputDeviceWin* device : devices_) { | |
| 70 max_size = std::max( | |
| 71 max_size, | |
| 72 static_cast<size_t>(device->viewport_pixel_size().GetArea() * 4)); | |
| 73 } | |
| 74 return max_size; | |
| 75 } | |
| 76 | |
| 77 std::vector<SoftwareOutputDeviceWin*> devices_; | |
| 78 scoped_ptr<base::SharedMemory> backing_; | |
| 79 size_t created_byte_size_; | |
| 80 friend struct DefaultSingletonTraits<SharedMemoryBacking>; | |
| 81 | |
| 82 DISALLOW_COPY_AND_ASSIGN(SharedMemoryBacking); | |
| 83 }; | |
| 84 | |
| 85 } // namespace | |
| 86 | |
| 18 SoftwareOutputDeviceWin::SoftwareOutputDeviceWin(ui::Compositor* compositor) | 87 SoftwareOutputDeviceWin::SoftwareOutputDeviceWin(ui::Compositor* compositor) |
| 19 : hwnd_(compositor->widget()), | 88 : hwnd_(compositor->widget()), |
| 20 is_hwnd_composited_(false) { | 89 is_hwnd_composited_(false) { |
| 21 // TODO(skaslev) Remove this when crbug.com/180702 is fixed. | 90 // TODO(skaslev) Remove this when crbug.com/180702 is fixed. |
| 22 DCHECK_CURRENTLY_ON(BrowserThread::UI); | 91 DCHECK_CURRENTLY_ON(BrowserThread::UI); |
| 23 | 92 |
| 24 LONG style = GetWindowLong(hwnd_, GWL_EXSTYLE); | 93 LONG style = GetWindowLong(hwnd_, GWL_EXSTYLE); |
| 25 is_hwnd_composited_ = !!(style & WS_EX_COMPOSITED); | 94 is_hwnd_composited_ = !!(style & WS_EX_COMPOSITED); |
| 95 if (!is_hwnd_composited_) | |
| 96 SharedMemoryBacking::GetInstance()->RegisterOutputDevice(this); | |
| 26 } | 97 } |
| 27 | 98 |
| 28 SoftwareOutputDeviceWin::~SoftwareOutputDeviceWin() { | 99 SoftwareOutputDeviceWin::~SoftwareOutputDeviceWin() { |
| 29 DCHECK_CURRENTLY_ON(BrowserThread::UI); | 100 DCHECK_CURRENTLY_ON(BrowserThread::UI); |
| 101 if (!is_hwnd_composited_) | |
| 102 SharedMemoryBacking::GetInstance()->UnregisterOutputDevice(this); | |
| 30 } | 103 } |
| 31 | 104 |
| 32 void SoftwareOutputDeviceWin::Resize(const gfx::Size& viewport_pixel_size, | 105 void SoftwareOutputDeviceWin::Resize(const gfx::Size& viewport_pixel_size, |
| 33 float scale_factor) { | 106 float scale_factor) { |
| 34 DCHECK_CURRENTLY_ON(BrowserThread::UI); | 107 DCHECK_CURRENTLY_ON(BrowserThread::UI); |
| 35 | 108 |
| 36 scale_factor_ = scale_factor; | 109 scale_factor_ = scale_factor; |
| 37 | 110 |
| 38 if (viewport_pixel_size_ == viewport_pixel_size) | 111 if (viewport_pixel_size_ == viewport_pixel_size) |
| 39 return; | 112 return; |
| 40 | 113 |
| 41 viewport_pixel_size_ = viewport_pixel_size; | 114 viewport_pixel_size_ = viewport_pixel_size; |
| 42 contents_.reset(new gfx::Canvas(viewport_pixel_size, 1.0f, true)); | 115 if (!is_hwnd_composited_) |
| 43 memset(&bitmap_info_, 0, sizeof(bitmap_info_)); | 116 SharedMemoryBacking::GetInstance()->Resized(); |
| 44 gfx::CreateBitmapHeader(viewport_pixel_size_.width(), | 117 contents_.reset(); |
| 45 viewport_pixel_size_.height(), | |
| 46 &bitmap_info_.bmiHeader); | |
| 47 } | 118 } |
| 48 | 119 |
| 49 SkCanvas* SoftwareOutputDeviceWin::BeginPaint(const gfx::Rect& damage_rect) { | 120 SkCanvas* SoftwareOutputDeviceWin::BeginPaint(const gfx::Rect& damage_rect) { |
| 50 DCHECK_CURRENTLY_ON(BrowserThread::UI); | 121 DCHECK_CURRENTLY_ON(BrowserThread::UI); |
| 51 DCHECK(contents_); | 122 if (!contents_) { |
| 123 HANDLE shared_section = NULL; | |
| 124 // Layered windows must be completely updated every time, so they can't | |
| 125 // share contents with other windows. | |
| 126 if (!is_hwnd_composited_) | |
|
piman
2015/05/11 23:20:56
nit: needs {}
| |
| 127 shared_section = | |
| 128 SharedMemoryBacking::GetInstance()->GetSharedMemory()->handle(); | |
| 129 contents_.reset(skia::CreatePlatformCanvas( | |
| 130 viewport_pixel_size_.width(), viewport_pixel_size_.height(), true, | |
| 131 shared_section, skia::CRASH_ON_FAILURE)); | |
| 132 } | |
| 52 | 133 |
| 53 damage_rect_ = damage_rect; | 134 damage_rect_ = damage_rect; |
| 54 return contents_ ? contents_->sk_canvas() : NULL; | 135 return contents_.get(); |
| 55 } | 136 } |
| 56 | 137 |
| 57 void SoftwareOutputDeviceWin::EndPaint(cc::SoftwareFrameData* frame_data) { | 138 void SoftwareOutputDeviceWin::EndPaint(cc::SoftwareFrameData* frame_data) { |
| 58 DCHECK_CURRENTLY_ON(BrowserThread::UI); | 139 DCHECK_CURRENTLY_ON(BrowserThread::UI); |
| 59 DCHECK(contents_); | 140 DCHECK(contents_); |
| 60 DCHECK(frame_data); | 141 DCHECK(frame_data); |
| 61 | 142 |
| 62 if (!contents_) | |
| 63 return; | |
| 64 | |
| 65 SoftwareOutputDevice::EndPaint(frame_data); | 143 SoftwareOutputDevice::EndPaint(frame_data); |
| 66 | 144 |
| 67 gfx::Rect rect = damage_rect_; | 145 gfx::Rect rect = damage_rect_; |
| 68 rect.Intersect(gfx::Rect(viewport_pixel_size_)); | 146 rect.Intersect(gfx::Rect(viewport_pixel_size_)); |
| 69 if (rect.IsEmpty()) | 147 if (rect.IsEmpty()) |
| 70 return; | 148 return; |
| 71 | 149 |
| 72 SkCanvas* canvas = contents_->sk_canvas(); | |
| 73 DCHECK(canvas); | |
| 74 if (is_hwnd_composited_) { | 150 if (is_hwnd_composited_) { |
| 75 RECT wr; | 151 RECT wr; |
| 76 GetWindowRect(hwnd_, &wr); | 152 GetWindowRect(hwnd_, &wr); |
| 77 SIZE size = {wr.right - wr.left, wr.bottom - wr.top}; | 153 SIZE size = {wr.right - wr.left, wr.bottom - wr.top}; |
| 78 POINT position = {wr.left, wr.top}; | 154 POINT position = {wr.left, wr.top}; |
| 79 POINT zero = {0, 0}; | 155 POINT zero = {0, 0}; |
| 80 BLENDFUNCTION blend = {AC_SRC_OVER, 0x00, 0xFF, AC_SRC_ALPHA}; | 156 BLENDFUNCTION blend = {AC_SRC_OVER, 0x00, 0xFF, AC_SRC_ALPHA}; |
| 81 | 157 |
| 82 DWORD style = GetWindowLong(hwnd_, GWL_EXSTYLE); | 158 DWORD style = GetWindowLong(hwnd_, GWL_EXSTYLE); |
| 83 style &= ~WS_EX_COMPOSITED; | 159 style &= ~WS_EX_COMPOSITED; |
| 84 style |= WS_EX_LAYERED; | 160 style |= WS_EX_LAYERED; |
| 85 SetWindowLong(hwnd_, GWL_EXSTYLE, style); | 161 SetWindowLong(hwnd_, GWL_EXSTYLE, style); |
| 86 | 162 |
| 87 HDC dib_dc = skia::BeginPlatformPaint(canvas); | 163 HDC dib_dc = skia::BeginPlatformPaint(contents_.get()); |
| 88 ::UpdateLayeredWindow(hwnd_, NULL, &position, &size, dib_dc, &zero, | 164 ::UpdateLayeredWindow(hwnd_, NULL, &position, &size, dib_dc, &zero, |
| 89 RGB(0xFF, 0xFF, 0xFF), &blend, ULW_ALPHA); | 165 RGB(0xFF, 0xFF, 0xFF), &blend, ULW_ALPHA); |
| 90 skia::EndPlatformPaint(canvas); | 166 skia::EndPlatformPaint(contents_.get()); |
| 91 } else { | 167 } else { |
| 92 HDC hdc = ::GetDC(hwnd_); | 168 HDC hdc = ::GetDC(hwnd_); |
| 93 RECT src_rect = rect.ToRECT(); | 169 RECT src_rect = rect.ToRECT(); |
| 94 skia::DrawToNativeContext(canvas, hdc, rect.x(), rect.y(), &src_rect); | 170 skia::DrawToNativeContext(contents_.get(), hdc, rect.x(), rect.y(), |
| 171 &src_rect); | |
| 95 ::ReleaseDC(hwnd_, hdc); | 172 ::ReleaseDC(hwnd_, hdc); |
| 96 } | 173 } |
| 97 } | 174 } |
| 98 | 175 |
| 176 void SoftwareOutputDeviceWin::ReleaseContents() { | |
|
piman
2015/05/11 23:20:56
nit: any way to DCHECK we're not between BeginPain
| |
| 177 contents_.release(); | |
| 178 } | |
| 179 | |
| 99 } // namespace content | 180 } // namespace content |
| OLD | NEW |