OLD | NEW |
---|---|
1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2011 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 "ui/gfx/compositor/compositor.h" | 5 #include "ui/gfx/compositor/compositor.h" |
6 | 6 |
7 #include <algorithm> | 7 #include <algorithm> |
8 #include <d3dx10.h> | 8 #include <d3dx10.h> |
9 #include <vector> | 9 #include <vector> |
10 | 10 |
(...skipping 37 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
48 class ViewTexture : public Texture { | 48 class ViewTexture : public Texture { |
49 public: | 49 public: |
50 ViewTexture(CompositorWin* compositor, | 50 ViewTexture(CompositorWin* compositor, |
51 ID3D10Device* device, | 51 ID3D10Device* device, |
52 ID3D10Effect* effect); | 52 ID3D10Effect* effect); |
53 | 53 |
54 // Texture: | 54 // Texture: |
55 virtual void SetCanvas(const SkCanvas& canvas, | 55 virtual void SetCanvas(const SkCanvas& canvas, |
56 const gfx::Point& origin, | 56 const gfx::Point& origin, |
57 const gfx::Size& overall_size) OVERRIDE; | 57 const gfx::Size& overall_size) OVERRIDE; |
58 | |
sky
2011/08/25 21:15:55
remove this, and 60. See the Compositor: section b
jonathan.backer
2011/08/26 11:45:55
Done.
| |
58 virtual void Draw(const ui::TextureDrawParams& params) OVERRIDE; | 59 virtual void Draw(const ui::TextureDrawParams& params) OVERRIDE; |
59 | 60 |
60 virtual void Draw(const ui::TextureDrawParams& params, | 61 virtual void Draw(const ui::TextureDrawParams& params, |
61 const gfx::Rect& clip_bounds_in_texture) OVERRIDE; | 62 const gfx::Rect& clip_bounds_in_texture) OVERRIDE; |
62 | 63 |
63 private: | 64 private: |
64 ~ViewTexture(); | 65 ~ViewTexture(); |
65 | 66 |
66 void Errored(HRESULT result); | 67 void Errored(HRESULT result); |
67 | 68 |
(...skipping 22 matching lines...) Expand all Loading... | |
90 CompositorWin(gfx::AcceleratedWidget widget, | 91 CompositorWin(gfx::AcceleratedWidget widget, |
91 const gfx::Size& size); | 92 const gfx::Size& size); |
92 | 93 |
93 void Init(); | 94 void Init(); |
94 | 95 |
95 // Invoked to update the perspective needed by this texture. |transform| is | 96 // Invoked to update the perspective needed by this texture. |transform| is |
96 // the transform for the texture, and |size| the size of the texture. | 97 // the transform for the texture, and |size| the size of the texture. |
97 void UpdatePerspective(const ui::Transform& transform, | 98 void UpdatePerspective(const ui::Transform& transform, |
98 const gfx::Size& view_size); | 99 const gfx::Size& view_size); |
99 | 100 |
100 // Returns the overall size of the compositor. | |
101 const gfx::Size& GetHostSize(); | |
102 | |
103 // Returns the index buffer used for drawing a texture. | 101 // Returns the index buffer used for drawing a texture. |
104 ID3D10Buffer* GetTextureIndexBuffer(); | 102 ID3D10Buffer* GetTextureIndexBuffer(); |
105 | 103 |
106 // Compositor: | 104 // Compositor: |
107 virtual Texture* CreateTexture() OVERRIDE; | 105 virtual Texture* CreateTexture() OVERRIDE; |
108 virtual void NotifyStart() OVERRIDE; | 106 virtual void NotifyStart() OVERRIDE; |
109 virtual void NotifyEnd() OVERRIDE; | 107 virtual void NotifyEnd() OVERRIDE; |
110 virtual void Blur(const gfx::Rect& bounds) OVERRIDE; | 108 virtual void Blur(const gfx::Rect& bounds) OVERRIDE; |
111 virtual void SchedulePaint() OVERRIDE; | 109 virtual void SchedulePaint() OVERRIDE; |
112 virtual void OnWidgetSizeChanged(const gfx::Size& size) OVERRIDE; | 110 |
111 protected: | |
112 virtual void OnWidgetSizeChanged() OVERRIDE; | |
113 | 113 |
114 private: | 114 private: |
115 enum Direction { | 115 enum Direction { |
116 HORIZONTAL, | 116 HORIZONTAL, |
117 VERTICAL | 117 VERTICAL |
118 }; | 118 }; |
119 | 119 |
120 ~CompositorWin(); | 120 ~CompositorWin(); |
121 | 121 |
122 void Errored(HRESULT error_code); | 122 void Errored(HRESULT error_code); |
123 | 123 |
124 void CreateDevice(); | 124 void CreateDevice(); |
125 | 125 |
126 void LoadEffects(); | 126 void LoadEffects(); |
127 | 127 |
128 void InitVertexLayout(); | 128 void InitVertexLayout(); |
129 | 129 |
130 void Resize(const gfx::Size& size); | |
131 | |
132 // Updates the kernel used for blurring. Size is the size of the texture | 130 // Updates the kernel used for blurring. Size is the size of the texture |
133 // being drawn to along the appropriate axis. | 131 // being drawn to along the appropriate axis. |
134 void UpdateBlurKernel(Direction direction, int size); | 132 void UpdateBlurKernel(Direction direction, int size); |
135 | 133 |
136 // Creates a texture, render target view and shader. | 134 // Creates a texture, render target view and shader. |
137 void CreateTexture(const gfx::Size& size, | 135 void CreateTexture(const gfx::Size& size, |
138 ID3D10Texture2D** texture, | 136 ID3D10Texture2D** texture, |
139 ID3D10RenderTargetView** render_target_view, | 137 ID3D10RenderTargetView** render_target_view, |
140 ID3D10ShaderResourceView** shader_resource_view); | 138 ID3D10ShaderResourceView** shader_resource_view); |
141 | 139 |
142 // Creates |vertex_buffer_|. | 140 // Creates |vertex_buffer_|. |
143 void CreateVertexBuffer(); | 141 void CreateVertexBuffer(); |
144 | 142 |
145 // Creates |index_buffer_|. | 143 // Creates |index_buffer_|. |
146 void CreateIndexBuffer(); | 144 void CreateIndexBuffer(); |
147 | 145 |
148 // Creates a vertex buffer for the specified region. The caller owns the | 146 // Creates a vertex buffer for the specified region. The caller owns the |
149 // return value. | 147 // return value. |
150 ID3D10Buffer* CreateVertexBufferForRegion(const gfx::Rect& bounds); | 148 ID3D10Buffer* CreateVertexBufferForRegion(const gfx::Rect& bounds); |
151 | 149 |
152 gfx::AcceleratedWidget host_; | 150 gfx::AcceleratedWidget host_; |
153 | 151 |
154 // Size the device was last created at. | |
155 gfx::Size last_size_; | |
156 | |
157 ScopedComPtr<ID3D10Device> device_; | 152 ScopedComPtr<ID3D10Device> device_; |
158 ScopedComPtr<IDXGISwapChain> swap_chain_; | 153 ScopedComPtr<IDXGISwapChain> swap_chain_; |
159 ScopedComPtr<ID3D10RenderTargetView> dest_render_target_view_; | 154 ScopedComPtr<ID3D10RenderTargetView> dest_render_target_view_; |
160 ScopedComPtr<ID3D10Texture2D> depth_stencil_buffer_; | 155 ScopedComPtr<ID3D10Texture2D> depth_stencil_buffer_; |
161 ScopedComPtr<ID3D10DepthStencilView> depth_stencil_view_; | 156 ScopedComPtr<ID3D10DepthStencilView> depth_stencil_view_; |
162 ScopedComPtr<ID3D10Effect, NULL> fx_; | 157 ScopedComPtr<ID3D10Effect, NULL> fx_; |
163 ID3D10EffectTechnique* technique_; | 158 ID3D10EffectTechnique* technique_; |
164 | 159 |
165 // Layout for Vertex. | 160 // Layout for Vertex. |
166 ScopedComPtr<ID3D10InputLayout> vertex_layout_; | 161 ScopedComPtr<ID3D10InputLayout> vertex_layout_; |
(...skipping 131 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
298 (SkColorGetA(color) << 24) | | 293 (SkColorGetA(color) << 24) | |
299 (SkColorGetB(color) << 16) | | 294 (SkColorGetB(color) << 16) | |
300 (SkColorGetG(color) << 8) | | 295 (SkColorGetG(color) << 8) | |
301 (SkColorGetR(color)); | 296 (SkColorGetR(color)); |
302 } | 297 } |
303 } | 298 } |
304 } | 299 } |
305 | 300 |
306 void ViewTexture::CreateVertexBuffer(const gfx::Size& size) { | 301 void ViewTexture::CreateVertexBuffer(const gfx::Size& size) { |
307 vertex_buffer_.Release(); | 302 vertex_buffer_.Release(); |
308 const gfx::Size& host_size = compositor_->GetHostSize(); | 303 const gfx::Size& host_size = compositor_->size(); |
309 float x = static_cast<float>(host_size.width()) / 2.0f; | 304 float x = static_cast<float>(host_size.width()) / 2.0f; |
310 float y = static_cast<float>(host_size.height()) / 2.0f; | 305 float y = static_cast<float>(host_size.height()) / 2.0f; |
311 float w = static_cast<float>(size.width()); | 306 float w = static_cast<float>(size.width()); |
312 float h = static_cast<float>(size.height()); | 307 float h = static_cast<float>(size.height()); |
313 Vertex vertices[] = { | 308 Vertex vertices[] = { |
314 { D3DXVECTOR3(0.0f, -h, 0.0f), D3DXVECTOR2(0.0f, 1.0f) }, | 309 { D3DXVECTOR3(0.0f, -h, 0.0f), D3DXVECTOR2(0.0f, 1.0f) }, |
315 { D3DXVECTOR3(0.0f, 0.0f, 0.0f), D3DXVECTOR2(0.0f, 0.0f) }, | 310 { D3DXVECTOR3(0.0f, 0.0f, 0.0f), D3DXVECTOR2(0.0f, 0.0f) }, |
316 { D3DXVECTOR3( w, 0.0f, 0.0f), D3DXVECTOR2(1.0f, 0.0f) }, | 311 { D3DXVECTOR3( w, 0.0f, 0.0f), D3DXVECTOR2(1.0f, 0.0f) }, |
317 { D3DXVECTOR3( w, -h, 0.0f), D3DXVECTOR2(1.0f, 1.0f) }, | 312 { D3DXVECTOR3( w, -h, 0.0f), D3DXVECTOR2(1.0f, 1.0f) }, |
318 }; | 313 }; |
319 | 314 |
320 // Create the vertex buffer containing the points. | 315 // Create the vertex buffer containing the points. |
321 D3D10_BUFFER_DESC buffer_desc; | 316 D3D10_BUFFER_DESC buffer_desc; |
322 buffer_desc.Usage = D3D10_USAGE_IMMUTABLE; | 317 buffer_desc.Usage = D3D10_USAGE_IMMUTABLE; |
323 buffer_desc.ByteWidth = sizeof(Vertex) * ARRAYSIZE_UNSAFE(vertices); | 318 buffer_desc.ByteWidth = sizeof(Vertex) * ARRAYSIZE_UNSAFE(vertices); |
324 buffer_desc.BindFlags = D3D10_BIND_VERTEX_BUFFER; | 319 buffer_desc.BindFlags = D3D10_BIND_VERTEX_BUFFER; |
325 buffer_desc.CPUAccessFlags = 0; | 320 buffer_desc.CPUAccessFlags = 0; |
326 buffer_desc.MiscFlags = 0; | 321 buffer_desc.MiscFlags = 0; |
327 D3D10_SUBRESOURCE_DATA init_data; | 322 D3D10_SUBRESOURCE_DATA init_data; |
328 init_data.pSysMem = vertices; | 323 init_data.pSysMem = vertices; |
329 RETURN_IF_FAILED(device_->CreateBuffer(&buffer_desc, &init_data, | 324 RETURN_IF_FAILED(device_->CreateBuffer(&buffer_desc, &init_data, |
330 vertex_buffer_.Receive())); | 325 vertex_buffer_.Receive())); |
331 } | 326 } |
332 | 327 |
333 CompositorWin::CompositorWin(gfx::AcceleratedWidget widget, | 328 CompositorWin::CompositorWin(gfx::AcceleratedWidget widget, |
334 const gfx::Size& size) | 329 const gfx::Size& size) |
335 : host_(widget), | 330 : Compositor(size), |
336 technique_(NULL), | 331 host_(widget), |
337 last_size_(size) { | 332 technique_(NULL) { |
338 } | 333 } |
339 | 334 |
340 void CompositorWin::Init() { | 335 void CompositorWin::Init() { |
341 CreateDevice(); | 336 CreateDevice(); |
342 LoadEffects(); | 337 LoadEffects(); |
343 Resize(last_size_); | 338 OnWidgetSizeChanged(); |
344 InitVertexLayout(); | 339 InitVertexLayout(); |
345 CreateVertexBuffer(); | 340 CreateVertexBuffer(); |
346 CreateIndexBuffer(); | 341 CreateIndexBuffer(); |
347 } | 342 } |
348 | 343 |
349 void CompositorWin::UpdatePerspective(const ui::Transform& transform, | 344 void CompositorWin::UpdatePerspective(const ui::Transform& transform, |
350 const gfx::Size& view_size) { | 345 const gfx::Size& view_size) { |
351 float transform_data_buffer[16]; | 346 float transform_data_buffer[16]; |
352 transform.matrix().asColMajorf(transform_data_buffer); | 347 transform.matrix().asColMajorf(transform_data_buffer); |
353 D3DXMATRIX transform_matrix(&transform_data_buffer[0]); | 348 D3DXMATRIX transform_matrix(&transform_data_buffer[0]); |
354 std::swap(transform_matrix._12, transform_matrix._21); | 349 std::swap(transform_matrix._12, transform_matrix._21); |
355 std::swap(transform_matrix._13, transform_matrix._31); | 350 std::swap(transform_matrix._13, transform_matrix._31); |
356 std::swap(transform_matrix._23, transform_matrix._32); | 351 std::swap(transform_matrix._23, transform_matrix._32); |
357 | 352 |
358 // Different coordinate system; flip the y. | 353 // Different coordinate system; flip the y. |
359 transform_matrix._42 *= -1; | 354 transform_matrix._42 *= -1; |
360 | 355 |
361 // Scale so x and y are from 0-2. | 356 // Scale so x and y are from 0-2. |
362 D3DXMATRIX scale_matrix; | 357 D3DXMATRIX scale_matrix; |
363 D3DXMatrixScaling( | 358 D3DXMatrixScaling( |
364 &scale_matrix, | 359 &scale_matrix, |
365 2.0f / static_cast<float>(last_size_.width()), | 360 2.0f / static_cast<float>(size().width()), |
366 2.0f / static_cast<float>(last_size_.height()), | 361 2.0f / static_cast<float>(size().height()), |
367 1.0f); | 362 1.0f); |
368 | 363 |
369 // Translate so x and y are from -1,-1 to 1,1. | 364 // Translate so x and y are from -1,-1 to 1,1. |
370 D3DXMATRIX translate_matrix; | 365 D3DXMATRIX translate_matrix; |
371 D3DXMatrixTranslation(&translate_matrix, -1.0f, 1.0f, 0.0f); | 366 D3DXMatrixTranslation(&translate_matrix, -1.0f, 1.0f, 0.0f); |
372 | 367 |
373 D3DXMATRIX projection_matrix; | 368 D3DXMATRIX projection_matrix; |
374 D3DXMatrixIdentity(&projection_matrix); | 369 D3DXMatrixIdentity(&projection_matrix); |
375 D3DXMatrixPerspectiveFovLH(&projection_matrix, | 370 D3DXMatrixPerspectiveFovLH(&projection_matrix, |
376 atanf(.5f) * 2.0f, 1.0f, 1.0f, 1000.0f); | 371 atanf(.5f) * 2.0f, 1.0f, 1.0f, 1000.0f); |
377 D3DXVECTOR3 pos(0.0f, 0.0f, -2.0f); | 372 D3DXVECTOR3 pos(0.0f, 0.0f, -2.0f); |
378 D3DXVECTOR3 target(0.0f, 0.0f, 0.0f); | 373 D3DXVECTOR3 target(0.0f, 0.0f, 0.0f); |
379 D3DXVECTOR3 up(0.0f, 1.0f, 0.0f); | 374 D3DXVECTOR3 up(0.0f, 1.0f, 0.0f); |
380 D3DXMATRIX view; | 375 D3DXMATRIX view; |
381 D3DXMatrixIdentity(&view); | 376 D3DXMatrixIdentity(&view); |
382 D3DXMatrixLookAtLH(&view, &pos, &target, &up); | 377 D3DXMatrixLookAtLH(&view, &pos, &target, &up); |
383 | 378 |
384 D3DXMATRIX wvp = transform_matrix * scale_matrix * translate_matrix * view * | 379 D3DXMATRIX wvp = transform_matrix * scale_matrix * translate_matrix * view * |
385 projection_matrix; | 380 projection_matrix; |
386 | 381 |
387 fx_->GetVariableByName("gWVP")->AsMatrix()->SetMatrix(wvp); | 382 fx_->GetVariableByName("gWVP")->AsMatrix()->SetMatrix(wvp); |
388 } | 383 } |
389 | 384 |
390 const gfx::Size& CompositorWin::GetHostSize() { | |
391 return last_size_; | |
392 } | |
393 | |
394 ID3D10Buffer* CompositorWin::GetTextureIndexBuffer() { | 385 ID3D10Buffer* CompositorWin::GetTextureIndexBuffer() { |
395 return index_buffer_.get(); | 386 return index_buffer_.get(); |
396 } | 387 } |
397 | 388 |
398 Texture* CompositorWin::CreateTexture() { | 389 Texture* CompositorWin::CreateTexture() { |
399 return new ViewTexture(this, device_.get(), fx_.get()); | 390 return new ViewTexture(this, device_.get(), fx_.get()); |
400 } | 391 } |
401 | 392 |
402 void CompositorWin::NotifyStart() { | 393 void CompositorWin::NotifyStart() { |
403 ID3D10RenderTargetView* target_view = main_render_target_view_.get(); | 394 ID3D10RenderTargetView* target_view = main_render_target_view_.get(); |
(...skipping 58 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
462 ID3D10Buffer* vertex_buffer = scoped_vertex_buffer.get(); | 453 ID3D10Buffer* vertex_buffer = scoped_vertex_buffer.get(); |
463 UINT stride = sizeof(Vertex); | 454 UINT stride = sizeof(Vertex); |
464 UINT offset = 0; | 455 UINT offset = 0; |
465 device_->IASetVertexBuffers(0, 1, &vertex_buffer, &stride, &offset); | 456 device_->IASetVertexBuffers(0, 1, &vertex_buffer, &stride, &offset); |
466 device_->IASetIndexBuffer(index_buffer_.get(), DXGI_FORMAT_R32_UINT, 0); | 457 device_->IASetIndexBuffer(index_buffer_.get(), DXGI_FORMAT_R32_UINT, 0); |
467 device_->DrawIndexed(6, 0, 0); | 458 device_->DrawIndexed(6, 0, 0); |
468 D3DXMATRIX identity_matrix; | 459 D3DXMATRIX identity_matrix; |
469 D3DXMatrixIdentity(&identity_matrix); | 460 D3DXMatrixIdentity(&identity_matrix); |
470 | 461 |
471 // Horizontal blur from the main texture to blur texture. | 462 // Horizontal blur from the main texture to blur texture. |
472 UpdateBlurKernel(HORIZONTAL, last_size_.width()); | 463 UpdateBlurKernel(HORIZONTAL, size().width()); |
473 ID3D10RenderTargetView* target_view = blur_render_target_view_.get(); | 464 ID3D10RenderTargetView* target_view = blur_render_target_view_.get(); |
474 device_->OMSetRenderTargets(1, &target_view, NULL); | 465 device_->OMSetRenderTargets(1, &target_view, NULL); |
475 RETURN_IF_FAILED( | 466 RETURN_IF_FAILED( |
476 blur_fx_->GetVariableByName("textureMap")->AsShaderResource()-> | 467 blur_fx_->GetVariableByName("textureMap")->AsShaderResource()-> |
477 SetResource(main_texture_shader_view_.get())); | 468 SetResource(main_texture_shader_view_.get())); |
478 blur_fx_->GetVariableByName("gWVP")->AsMatrix()->SetMatrix( | 469 blur_fx_->GetVariableByName("gWVP")->AsMatrix()->SetMatrix( |
479 identity_matrix); | 470 identity_matrix); |
480 ID3D10EffectTechnique* technique = blur_fx_->GetTechniqueByName("ViewTech"); | 471 ID3D10EffectTechnique* technique = blur_fx_->GetTechniqueByName("ViewTech"); |
481 DCHECK(technique); | 472 DCHECK(technique); |
482 D3D10_TECHNIQUE_DESC tech_desc; | 473 D3D10_TECHNIQUE_DESC tech_desc; |
483 technique->GetDesc(&tech_desc); | 474 technique->GetDesc(&tech_desc); |
484 for(UINT p = 0; p < tech_desc.Passes; ++p) | 475 for(UINT p = 0; p < tech_desc.Passes; ++p) |
485 technique->GetPassByIndex(p)->Apply(0); | 476 technique->GetPassByIndex(p)->Apply(0); |
486 device_->DrawIndexed(6, 0, 0); | 477 device_->DrawIndexed(6, 0, 0); |
487 | 478 |
488 { | 479 { |
489 // We do this to avoid a warning. | 480 // We do this to avoid a warning. |
490 ID3D10ShaderResourceView* tmp = NULL; | 481 ID3D10ShaderResourceView* tmp = NULL; |
491 device_->PSSetShaderResources(0, 1, &tmp); | 482 device_->PSSetShaderResources(0, 1, &tmp); |
492 } | 483 } |
493 | 484 |
494 // Vertical blur from the blur texture back to main buffer. | 485 // Vertical blur from the blur texture back to main buffer. |
495 RETURN_IF_FAILED( | 486 RETURN_IF_FAILED( |
496 blur_fx_->GetVariableByName("textureMap")->AsShaderResource()-> | 487 blur_fx_->GetVariableByName("textureMap")->AsShaderResource()-> |
497 SetResource(blur_texture_shader_view_.get())); | 488 SetResource(blur_texture_shader_view_.get())); |
498 UpdateBlurKernel(VERTICAL, last_size_.height()); | 489 UpdateBlurKernel(VERTICAL, size().height()); |
499 target_view = main_render_target_view_.get(); | 490 target_view = main_render_target_view_.get(); |
500 device_->OMSetRenderTargets(1, &target_view, NULL); | 491 device_->OMSetRenderTargets(1, &target_view, NULL); |
501 for(UINT p = 0; p < tech_desc.Passes; ++p) | 492 for(UINT p = 0; p < tech_desc.Passes; ++p) |
502 technique->GetPassByIndex(p)->Apply(0); | 493 technique->GetPassByIndex(p)->Apply(0); |
503 device_->DrawIndexed(6, 0, 0); | 494 device_->DrawIndexed(6, 0, 0); |
504 | 495 |
505 #if !defined(NDEBUG) | 496 #if !defined(NDEBUG) |
506 // A warning is generated if a bound vertexbuffer is deleted. To avoid that | 497 // A warning is generated if a bound vertexbuffer is deleted. To avoid that |
507 // warning we reset the buffer here. We only do it for debug builds as it's | 498 // warning we reset the buffer here. We only do it for debug builds as it's |
508 // ok to effectively unbind the vertex buffer. | 499 // ok to effectively unbind the vertex buffer. |
509 vertex_buffer = vertex_buffer_.get(); | 500 vertex_buffer = vertex_buffer_.get(); |
510 device_->IASetVertexBuffers(0, 1, &vertex_buffer, &stride, &offset); | 501 device_->IASetVertexBuffers(0, 1, &vertex_buffer, &stride, &offset); |
511 device_->IASetIndexBuffer(index_buffer_.get(), DXGI_FORMAT_R32_UINT, 0); | 502 device_->IASetIndexBuffer(index_buffer_.get(), DXGI_FORMAT_R32_UINT, 0); |
512 #endif | 503 #endif |
513 } | 504 } |
514 | 505 |
515 void CompositorWin::SchedulePaint() { | 506 void CompositorWin::SchedulePaint() { |
516 RECT bounds; | 507 RECT bounds; |
517 GetClientRect(host_, &bounds); | 508 GetClientRect(host_, &bounds); |
518 InvalidateRect(host_, &bounds, FALSE); | 509 InvalidateRect(host_, &bounds, FALSE); |
519 } | 510 } |
520 | 511 |
521 void CompositorWin::OnWidgetSizeChanged(const gfx::Size& size) { | 512 void CompositorWin::OnWidgetSizeChanged() { |
522 Resize(size); | 513 dest_render_target_view_ = NULL; |
514 depth_stencil_buffer_ = NULL; | |
515 depth_stencil_view_ = NULL; | |
516 | |
517 main_render_target_view_ = NULL; | |
518 main_texture_ = NULL; | |
519 main_texture_shader_view_ = NULL; | |
520 | |
521 blur_render_target_view_ = NULL; | |
522 blur_texture_ = NULL; | |
523 blur_texture_shader_view_ = NULL; | |
524 | |
525 CreateTexture(size(), main_texture_.Receive(), | |
526 main_render_target_view_.Receive(), | |
527 main_texture_shader_view_.Receive()); | |
528 | |
529 CreateTexture(size(), blur_texture_.Receive(), | |
530 blur_render_target_view_.Receive(), | |
531 blur_texture_shader_view_.Receive()); | |
532 | |
533 // Resize the swap chain and recreate the render target view. | |
534 RETURN_IF_FAILED(swap_chain_->ResizeBuffers( | |
535 1, size().width(), size().height(), DXGI_FORMAT_R8G8B8A8_UNORM, 0)); | |
536 ScopedComPtr<ID3D10Texture2D> back_buffer; | |
537 RETURN_IF_FAILED(swap_chain_->GetBuffer( | |
538 0, __uuidof(ID3D10Texture2D), | |
539 reinterpret_cast<void**>(back_buffer.Receive()))); | |
540 RETURN_IF_FAILED(device_->CreateRenderTargetView( | |
541 back_buffer.get(), 0, | |
542 dest_render_target_view_.Receive())); | |
543 | |
544 // Create the depth/stencil buffer and view. | |
545 D3D10_TEXTURE2D_DESC depth_stencil_desc; | |
546 depth_stencil_desc.Width = size().width(); | |
547 depth_stencil_desc.Height = size().height(); | |
548 depth_stencil_desc.MipLevels = 1; | |
549 depth_stencil_desc.ArraySize = 1; | |
550 depth_stencil_desc.Format = DXGI_FORMAT_D24_UNORM_S8_UINT; | |
551 depth_stencil_desc.SampleDesc.Count = 1; // multisampling must match | |
552 depth_stencil_desc.SampleDesc.Quality = 0; // swap chain values. | |
553 depth_stencil_desc.Usage = D3D10_USAGE_DEFAULT; | |
554 depth_stencil_desc.BindFlags = D3D10_BIND_DEPTH_STENCIL; | |
555 depth_stencil_desc.CPUAccessFlags = 0; | |
556 depth_stencil_desc.MiscFlags = 0; | |
557 | |
558 RETURN_IF_FAILED(device_->CreateTexture2D(&depth_stencil_desc, 0, | |
559 depth_stencil_buffer_.Receive())); | |
560 RETURN_IF_FAILED(device_->CreateDepthStencilView( | |
561 depth_stencil_buffer_.get(), 0, | |
562 depth_stencil_view_.Receive())); | |
563 | |
564 | |
565 // Set the viewport transform. | |
566 D3D10_VIEWPORT vp; | |
567 vp.TopLeftX = 0; | |
568 vp.TopLeftY = 0; | |
569 vp.Width = size().width(); | |
570 vp.Height = size().height(); | |
571 vp.MinDepth = 0.0f; | |
572 vp.MaxDepth = 1.0f; | |
573 | |
574 device_->RSSetViewports(1, &vp); | |
523 } | 575 } |
524 | 576 |
525 CompositorWin::~CompositorWin() { | 577 CompositorWin::~CompositorWin() { |
526 } | 578 } |
527 | 579 |
528 void CompositorWin::Errored(HRESULT error_code) { | 580 void CompositorWin::Errored(HRESULT error_code) { |
529 // TODO: figure out error handling. | 581 // TODO: figure out error handling. |
530 DCHECK(false); | 582 DCHECK(false); |
531 } | 583 } |
532 | 584 |
533 void CompositorWin::CreateDevice() { | 585 void CompositorWin::CreateDevice() { |
534 DXGI_SWAP_CHAIN_DESC sd; | 586 DXGI_SWAP_CHAIN_DESC sd; |
535 sd.BufferDesc.Width = last_size_.width(); | 587 sd.BufferDesc.Width = size().width(); |
536 sd.BufferDesc.Height = last_size_.height(); | 588 sd.BufferDesc.Height = size().height(); |
537 sd.BufferDesc.RefreshRate.Numerator = 60; | 589 sd.BufferDesc.RefreshRate.Numerator = 60; |
538 sd.BufferDesc.RefreshRate.Denominator = 1; | 590 sd.BufferDesc.RefreshRate.Denominator = 1; |
539 sd.BufferDesc.Format = DXGI_FORMAT_R8G8B8A8_UNORM; | 591 sd.BufferDesc.Format = DXGI_FORMAT_R8G8B8A8_UNORM; |
540 sd.BufferDesc.ScanlineOrdering = DXGI_MODE_SCANLINE_ORDER_UNSPECIFIED; | 592 sd.BufferDesc.ScanlineOrdering = DXGI_MODE_SCANLINE_ORDER_UNSPECIFIED; |
541 sd.BufferDesc.Scaling = DXGI_MODE_SCALING_UNSPECIFIED; | 593 sd.BufferDesc.Scaling = DXGI_MODE_SCALING_UNSPECIFIED; |
542 | 594 |
543 // No multisampling. | 595 // No multisampling. |
544 sd.SampleDesc.Count = 1; | 596 sd.SampleDesc.Count = 1; |
545 sd.SampleDesc.Quality = 0; | 597 sd.SampleDesc.Quality = 0; |
546 | 598 |
(...skipping 62 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
609 // Create the input layout | 661 // Create the input layout |
610 D3D10_PASS_DESC pass_desc; | 662 D3D10_PASS_DESC pass_desc; |
611 RETURN_IF_FAILED(technique_->GetPassByIndex(0)->GetDesc(&pass_desc)); | 663 RETURN_IF_FAILED(technique_->GetPassByIndex(0)->GetDesc(&pass_desc)); |
612 RETURN_IF_FAILED( | 664 RETURN_IF_FAILED( |
613 device_->CreateInputLayout(vertex_desc, ARRAYSIZE_UNSAFE(vertex_desc), | 665 device_->CreateInputLayout(vertex_desc, ARRAYSIZE_UNSAFE(vertex_desc), |
614 pass_desc.pIAInputSignature, | 666 pass_desc.pIAInputSignature, |
615 pass_desc.IAInputSignatureSize, | 667 pass_desc.IAInputSignatureSize, |
616 vertex_layout_.Receive())); | 668 vertex_layout_.Receive())); |
617 } | 669 } |
618 | 670 |
619 void CompositorWin::Resize(const gfx::Size& size) { | |
620 last_size_ = size; | |
621 | |
622 dest_render_target_view_ = NULL; | |
623 depth_stencil_buffer_ = NULL; | |
624 depth_stencil_view_ = NULL; | |
625 | |
626 main_render_target_view_ = NULL; | |
627 main_texture_ = NULL; | |
628 main_texture_shader_view_ = NULL; | |
629 | |
630 blur_render_target_view_ = NULL; | |
631 blur_texture_ = NULL; | |
632 blur_texture_shader_view_ = NULL; | |
633 | |
634 CreateTexture(size, main_texture_.Receive(), | |
635 main_render_target_view_.Receive(), | |
636 main_texture_shader_view_.Receive()); | |
637 | |
638 CreateTexture(size, blur_texture_.Receive(), | |
639 blur_render_target_view_.Receive(), | |
640 blur_texture_shader_view_.Receive()); | |
641 | |
642 // Resize the swap chain and recreate the render target view. | |
643 RETURN_IF_FAILED(swap_chain_->ResizeBuffers( | |
644 1, size.width(), size.height(), DXGI_FORMAT_R8G8B8A8_UNORM, 0)); | |
645 ScopedComPtr<ID3D10Texture2D> back_buffer; | |
646 RETURN_IF_FAILED(swap_chain_->GetBuffer( | |
647 0, __uuidof(ID3D10Texture2D), | |
648 reinterpret_cast<void**>(back_buffer.Receive()))); | |
649 RETURN_IF_FAILED(device_->CreateRenderTargetView( | |
650 back_buffer.get(), 0, | |
651 dest_render_target_view_.Receive())); | |
652 | |
653 // Create the depth/stencil buffer and view. | |
654 D3D10_TEXTURE2D_DESC depth_stencil_desc; | |
655 depth_stencil_desc.Width = size.width(); | |
656 depth_stencil_desc.Height = size.height(); | |
657 depth_stencil_desc.MipLevels = 1; | |
658 depth_stencil_desc.ArraySize = 1; | |
659 depth_stencil_desc.Format = DXGI_FORMAT_D24_UNORM_S8_UINT; | |
660 depth_stencil_desc.SampleDesc.Count = 1; // multisampling must match | |
661 depth_stencil_desc.SampleDesc.Quality = 0; // swap chain values. | |
662 depth_stencil_desc.Usage = D3D10_USAGE_DEFAULT; | |
663 depth_stencil_desc.BindFlags = D3D10_BIND_DEPTH_STENCIL; | |
664 depth_stencil_desc.CPUAccessFlags = 0; | |
665 depth_stencil_desc.MiscFlags = 0; | |
666 | |
667 RETURN_IF_FAILED(device_->CreateTexture2D(&depth_stencil_desc, 0, | |
668 depth_stencil_buffer_.Receive())); | |
669 RETURN_IF_FAILED(device_->CreateDepthStencilView( | |
670 depth_stencil_buffer_.get(), 0, | |
671 depth_stencil_view_.Receive())); | |
672 | |
673 | |
674 // Set the viewport transform. | |
675 D3D10_VIEWPORT vp; | |
676 vp.TopLeftX = 0; | |
677 vp.TopLeftY = 0; | |
678 vp.Width = size.width(); | |
679 vp.Height = size.height(); | |
680 vp.MinDepth = 0.0f; | |
681 vp.MaxDepth = 1.0f; | |
682 | |
683 device_->RSSetViewports(1, &vp); | |
684 } | |
685 | |
686 void CompositorWin::UpdateBlurKernel(Direction direction, int size) { | 671 void CompositorWin::UpdateBlurKernel(Direction direction, int size) { |
687 // Update the blur data. | 672 // Update the blur data. |
688 const int kSize = 13; | 673 const int kSize = 13; |
689 float pixel_data[4 * kSize]; | 674 float pixel_data[4 * kSize]; |
690 memset(pixel_data, 0, sizeof(float) * 4 * kSize); | 675 memset(pixel_data, 0, sizeof(float) * 4 * kSize); |
691 for (int i = 0; i < kSize; ++i) { | 676 for (int i = 0; i < kSize; ++i) { |
692 pixel_data[i * 4 + ((direction == HORIZONTAL) ? 0 : 1)] = | 677 pixel_data[i * 4 + ((direction == HORIZONTAL) ? 0 : 1)] = |
693 static_cast<float>(i - (kSize / 2)) / static_cast<float>(size); | 678 static_cast<float>(i - (kSize / 2)) / static_cast<float>(size); |
694 } | 679 } |
695 RETURN_IF_FAILED(blur_fx_->GetVariableByName("TexelKernel")->AsVector()-> | 680 RETURN_IF_FAILED(blur_fx_->GetVariableByName("TexelKernel")->AsVector()-> |
(...skipping 70 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
766 index_buffer.MiscFlags = 0; | 751 index_buffer.MiscFlags = 0; |
767 D3D10_SUBRESOURCE_DATA init_data; | 752 D3D10_SUBRESOURCE_DATA init_data; |
768 init_data.pSysMem = indices; | 753 init_data.pSysMem = indices; |
769 RETURN_IF_FAILED(device_->CreateBuffer(&index_buffer, &init_data, | 754 RETURN_IF_FAILED(device_->CreateBuffer(&index_buffer, &init_data, |
770 index_buffer_.Receive())); | 755 index_buffer_.Receive())); |
771 } | 756 } |
772 | 757 |
773 ID3D10Buffer* CompositorWin::CreateVertexBufferForRegion( | 758 ID3D10Buffer* CompositorWin::CreateVertexBufferForRegion( |
774 const gfx::Rect& bounds) { | 759 const gfx::Rect& bounds) { |
775 float x = static_cast<float>(bounds.x()) / | 760 float x = static_cast<float>(bounds.x()) / |
776 static_cast<float>(last_size_.width()) * 2.0f - 1.0f; | 761 static_cast<float>(size().width()) * 2.0f - 1.0f; |
777 float max_x = | 762 float max_x = |
778 x + bounds.width() / static_cast<float>(last_size_.width()) * 2.0f; | 763 x + bounds.width() / static_cast<float>(size().width()) * 2.0f; |
779 float y = | 764 float y = |
780 static_cast<float>(last_size_.height() - bounds.y() - bounds.height()) / | 765 static_cast<float>(size().height() - bounds.y() - bounds.height()) / |
781 static_cast<float>(last_size_.height()) * 2.0f - 1.0f; | 766 static_cast<float>(size().height()) * 2.0f - 1.0f; |
782 float max_y = | 767 float max_y = |
783 y + bounds.height() / static_cast<float>(last_size_.height()) * 2.0f; | 768 y + bounds.height() / static_cast<float>(size().height()) * 2.0f; |
784 float tex_x = x / 2.0f + .5f; | 769 float tex_x = x / 2.0f + .5f; |
785 float max_tex_x = max_x / 2.0f + .5f; | 770 float max_tex_x = max_x / 2.0f + .5f; |
786 float tex_y = 1.0f - (max_y + 1.0f) / 2.0f; | 771 float tex_y = 1.0f - (max_y + 1.0f) / 2.0f; |
787 float max_tex_y = tex_y + (max_y - y) / 2.0f; | 772 float max_tex_y = tex_y + (max_y - y) / 2.0f; |
788 Vertex vertices[] = { | 773 Vertex vertices[] = { |
789 { D3DXVECTOR3( x, y, 0.0f), D3DXVECTOR2( tex_x, max_tex_y) }, | 774 { D3DXVECTOR3( x, y, 0.0f), D3DXVECTOR2( tex_x, max_tex_y) }, |
790 { D3DXVECTOR3( x, max_y, 0.0f), D3DXVECTOR2( tex_x, tex_y) }, | 775 { D3DXVECTOR3( x, max_y, 0.0f), D3DXVECTOR2( tex_x, tex_y) }, |
791 { D3DXVECTOR3(max_x, max_y, 0.0f), D3DXVECTOR2(max_tex_x, tex_y) }, | 776 { D3DXVECTOR3(max_x, max_y, 0.0f), D3DXVECTOR2(max_tex_x, tex_y) }, |
792 { D3DXVECTOR3(max_x, y, 0.0f), D3DXVECTOR2(max_tex_x, max_tex_y) }, | 777 { D3DXVECTOR3(max_x, y, 0.0f), D3DXVECTOR2(max_tex_x, max_tex_y) }, |
793 }; | 778 }; |
(...skipping 22 matching lines...) Expand all Loading... | |
816 | 801 |
817 // static | 802 // static |
818 Compositor* Compositor::Create(gfx::AcceleratedWidget widget, | 803 Compositor* Compositor::Create(gfx::AcceleratedWidget widget, |
819 const gfx::Size& size) { | 804 const gfx::Size& size) { |
820 CompositorWin* compositor = new CompositorWin(widget, size); | 805 CompositorWin* compositor = new CompositorWin(widget, size); |
821 compositor->Init(); | 806 compositor->Init(); |
822 return compositor; | 807 return compositor; |
823 } | 808 } |
824 | 809 |
825 } // namespace ui | 810 } // namespace ui |
OLD | NEW |