| OLD | NEW |
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 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 | 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 "base/basictypes.h" | 5 #include "base/basictypes.h" |
| 6 #include "base/bind.h" | 6 #include "base/bind.h" |
| 7 #include "base/compiler_specific.h" | 7 #include "base/compiler_specific.h" |
| 8 #include "base/debug/trace_event.h" | 8 #include "base/debug/trace_event.h" |
| 9 #include "base/file_util.h" | 9 #include "base/file_util.h" |
| 10 #include "base/files/file_path.h" | 10 #include "base/files/file_path.h" |
| 11 #include "base/json/json_reader.h" | 11 #include "base/json/json_reader.h" |
| 12 #include "base/memory/scoped_ptr.h" | 12 #include "base/memory/scoped_ptr.h" |
| 13 #include "base/message_loop/message_loop.h" | 13 #include "base/message_loop/message_loop.h" |
| 14 #include "base/path_service.h" | 14 #include "base/path_service.h" |
| 15 #include "base/strings/string_util.h" | 15 #include "base/strings/string_util.h" |
| 16 #include "base/strings/stringprintf.h" | 16 #include "base/strings/stringprintf.h" |
| 17 #include "cc/layers/delegated_frame_provider.h" | 17 #include "cc/layers/delegated_frame_provider.h" |
| 18 #include "cc/layers/delegated_frame_resource_collection.h" | 18 #include "cc/layers/delegated_frame_resource_collection.h" |
| 19 #include "cc/layers/layer.h" | 19 #include "cc/layers/layer.h" |
| 20 #include "cc/output/copy_output_request.h" |
| 21 #include "cc/output/copy_output_result.h" |
| 20 #include "cc/output/delegated_frame_data.h" | 22 #include "cc/output/delegated_frame_data.h" |
| 21 #include "cc/test/pixel_test_utils.h" | 23 #include "cc/test/pixel_test_utils.h" |
| 22 #include "testing/gtest/include/gtest/gtest.h" | 24 #include "testing/gtest/include/gtest/gtest.h" |
| 23 #include "ui/compositor/compositor_observer.h" | 25 #include "ui/compositor/compositor_observer.h" |
| 24 #include "ui/compositor/layer.h" | 26 #include "ui/compositor/layer.h" |
| 25 #include "ui/compositor/layer_animation_sequence.h" | 27 #include "ui/compositor/layer_animation_sequence.h" |
| 26 #include "ui/compositor/layer_animator.h" | 28 #include "ui/compositor/layer_animator.h" |
| 27 #include "ui/compositor/test/context_factories_for_test.h" | 29 #include "ui/compositor/test/context_factories_for_test.h" |
| 28 #include "ui/compositor/test/draw_waiter_for_test.h" | 30 #include "ui/compositor/test/draw_waiter_for_test.h" |
| 29 #include "ui/compositor/test/test_compositor_host.h" | 31 #include "ui/compositor/test/test_compositor_host.h" |
| (...skipping 92 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 122 return layer; | 124 return layer; |
| 123 } | 125 } |
| 124 | 126 |
| 125 void DrawTree(Layer* root) { | 127 void DrawTree(Layer* root) { |
| 126 GetCompositor()->SetRootLayer(root); | 128 GetCompositor()->SetRootLayer(root); |
| 127 GetCompositor()->ScheduleDraw(); | 129 GetCompositor()->ScheduleDraw(); |
| 128 WaitForDraw(); | 130 WaitForDraw(); |
| 129 } | 131 } |
| 130 | 132 |
| 131 bool ReadPixels(SkBitmap* bitmap) { | 133 bool ReadPixels(SkBitmap* bitmap) { |
| 132 return GetCompositor()->ReadPixels(bitmap, | 134 return ReadPixels(bitmap, gfx::Rect(GetCompositor()->size())); |
| 133 gfx::Rect(GetCompositor()->size())); | 135 } |
| 136 |
| 137 bool ReadPixels(SkBitmap* bitmap, gfx::Rect source_rect) { |
| 138 scoped_refptr<ReadbackHolder> holder(new ReadbackHolder); |
| 139 scoped_ptr<cc::CopyOutputRequest> request = |
| 140 cc::CopyOutputRequest::CreateBitmapRequest( |
| 141 base::Bind(&ReadbackHolder::OutputRequestCallback, holder)); |
| 142 request->set_area(source_rect); |
| 143 |
| 144 GetCompositor()->root_layer()->RequestCopyOfOutput(request.Pass()); |
| 145 |
| 146 // Wait for copy response. This needs to wait as the compositor could |
| 147 // be in the middle of a draw right now, and the commit with the |
| 148 // copy output request may not be done on the first draw. |
| 149 for (int i = 0; i < 2; i++) { |
| 150 GetCompositor()->ScheduleDraw(); |
| 151 WaitForDraw(); |
| 152 } |
| 153 |
| 154 if (holder->completed()) { |
| 155 *bitmap = holder->result(); |
| 156 return true; |
| 157 } |
| 158 |
| 159 // Callback never called. |
| 160 NOTREACHED(); |
| 161 return false; |
| 134 } | 162 } |
| 135 | 163 |
| 136 void WaitForDraw() { | 164 void WaitForDraw() { |
| 137 ui::DrawWaiterForTest::Wait(GetCompositor()); | 165 ui::DrawWaiterForTest::Wait(GetCompositor()); |
| 138 } | 166 } |
| 139 | 167 |
| 140 void WaitForCommit() { | 168 void WaitForCommit() { |
| 141 ui::DrawWaiterForTest::WaitForCommit(GetCompositor()); | 169 ui::DrawWaiterForTest::WaitForCommit(GetCompositor()); |
| 142 } | 170 } |
| 143 | 171 |
| 144 // Invalidates the entire contents of the layer. | 172 // Invalidates the entire contents of the layer. |
| 145 void SchedulePaintForLayer(Layer* layer) { | 173 void SchedulePaintForLayer(Layer* layer) { |
| 146 layer->SchedulePaint( | 174 layer->SchedulePaint( |
| 147 gfx::Rect(0, 0, layer->bounds().width(), layer->bounds().height())); | 175 gfx::Rect(0, 0, layer->bounds().width(), layer->bounds().height())); |
| 148 } | 176 } |
| 149 | 177 |
| 150 const base::FilePath& test_data_directory() const { | 178 const base::FilePath& test_data_directory() const { |
| 151 return test_data_directory_; | 179 return test_data_directory_; |
| 152 } | 180 } |
| 153 | 181 |
| 154 private: | 182 private: |
| 183 class ReadbackHolder : public base::RefCountedThreadSafe<ReadbackHolder> { |
| 184 public: |
| 185 ReadbackHolder() : completed_(false) {} |
| 186 |
| 187 void OutputRequestCallback(scoped_ptr<cc::CopyOutputResult> result) { |
| 188 DCHECK(!completed_); |
| 189 result_ = result->TakeBitmap(); |
| 190 completed_ = true; |
| 191 } |
| 192 bool completed() const { |
| 193 return completed_; |
| 194 }; |
| 195 const SkBitmap& result() const { return *result_; } |
| 196 |
| 197 private: |
| 198 friend class base::RefCountedThreadSafe<ReadbackHolder>; |
| 199 |
| 200 virtual ~ReadbackHolder() {} |
| 201 |
| 202 scoped_ptr<SkBitmap> result_; |
| 203 bool completed_; |
| 204 }; |
| 205 |
| 155 scoped_ptr<TestCompositorHost> window_; | 206 scoped_ptr<TestCompositorHost> window_; |
| 156 | 207 |
| 157 // The root directory for test files. | 208 // The root directory for test files. |
| 158 base::FilePath test_data_directory_; | 209 base::FilePath test_data_directory_; |
| 159 | 210 |
| 160 DISALLOW_COPY_AND_ASSIGN(LayerWithRealCompositorTest); | 211 DISALLOW_COPY_AND_ASSIGN(LayerWithRealCompositorTest); |
| 161 }; | 212 }; |
| 162 | 213 |
| 163 // LayerDelegate that paints colors to the layer. | 214 // LayerDelegate that paints colors to the layer. |
| 164 class TestLayerDelegate : public LayerDelegate { | 215 class TestLayerDelegate : public LayerDelegate { |
| (...skipping 634 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 799 CreateColorLayer(SK_ColorRED, gfx::Rect(viewport_size))); | 850 CreateColorLayer(SK_ColorRED, gfx::Rect(viewport_size))); |
| 800 scoped_ptr<Layer> layer2( | 851 scoped_ptr<Layer> layer2( |
| 801 CreateColorLayer(SK_ColorBLUE, | 852 CreateColorLayer(SK_ColorBLUE, |
| 802 gfx::Rect(0, 0, viewport_size.width(), blue_height))); | 853 gfx::Rect(0, 0, viewport_size.width(), blue_height))); |
| 803 | 854 |
| 804 layer->Add(layer2.get()); | 855 layer->Add(layer2.get()); |
| 805 | 856 |
| 806 DrawTree(layer.get()); | 857 DrawTree(layer.get()); |
| 807 | 858 |
| 808 SkBitmap bitmap; | 859 SkBitmap bitmap; |
| 809 ASSERT_TRUE(GetCompositor()->ReadPixels(&bitmap, gfx::Rect(viewport_size))); | 860 ASSERT_TRUE(ReadPixels(&bitmap, gfx::Rect(viewport_size))); |
| 810 ASSERT_FALSE(bitmap.empty()); | 861 ASSERT_FALSE(bitmap.empty()); |
| 811 | 862 |
| 812 SkAutoLockPixels lock(bitmap); | 863 SkAutoLockPixels lock(bitmap); |
| 813 for (int x = 0; x < viewport_size.width(); x++) { | 864 for (int x = 0; x < viewport_size.width(); x++) { |
| 814 for (int y = 0; y < viewport_size.height(); y++) { | 865 for (int y = 0; y < viewport_size.height(); y++) { |
| 815 SkColor actual_color = bitmap.getColor(x, y); | 866 SkColor actual_color = bitmap.getColor(x, y); |
| 816 SkColor expected_color = y < blue_height ? SK_ColorBLUE : SK_ColorRED; | 867 SkColor expected_color = y < blue_height ? SK_ColorBLUE : SK_ColorRED; |
| 817 EXPECT_EQ(expected_color, actual_color) | 868 EXPECT_EQ(expected_color, actual_color) |
| 818 << "Pixel error at x=" << x << " y=" << y << "; " | 869 << "Pixel error at x=" << x << " y=" << y << "; " |
| 819 << "actual RGBA=(" | 870 << "actual RGBA=(" |
| (...skipping 645 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1465 l1->SetOpacity(0.5f); | 1516 l1->SetOpacity(0.5f); |
| 1466 | 1517 |
| 1467 // Change l1's cc::Layer. | 1518 // Change l1's cc::Layer. |
| 1468 l1->SwitchCCLayerForTest(); | 1519 l1->SwitchCCLayerForTest(); |
| 1469 | 1520 |
| 1470 // Ensure that the opacity animation completed. | 1521 // Ensure that the opacity animation completed. |
| 1471 EXPECT_FLOAT_EQ(l1->opacity(), 0.5f); | 1522 EXPECT_FLOAT_EQ(l1->opacity(), 0.5f); |
| 1472 } | 1523 } |
| 1473 | 1524 |
| 1474 } // namespace ui | 1525 } // namespace ui |
| OLD | NEW |