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 3 // found in the LICENSE file. 4 5 #include "content/renderer/pepper/pepper_graphics_2d_host.h" 6 7 #include "base/basictypes.h" 8 #include "base/message_loop/message_loop.h" 9 #include "content/renderer/pepper/gfx_conversion.h" 10 #include "content/renderer/pepper/mock_renderer_ppapi_host.h" 11 #include "content/renderer/pepper/ppb_image_data_impl.h" 12 #include "ppapi/shared_impl/ppb_view_shared.h" 13 #include "ppapi/shared_impl/proxy_lock.h" 14 #include "ppapi/shared_impl/test_globals.h" 15 #include "testing/gtest/include/gtest/gtest.h" 16 #include "third_party/WebKit/public/platform/WebCanvas.h" 17 #include "third_party/skia/include/core/SkCanvas.h" 18 #include "ui/events/latency_info.h" 19 #include "ui/gfx/point.h" 20 #include "ui/gfx/rect.h" 21 22 using blink::WebCanvas; 23 24 namespace content { 25 26 class PepperGraphics2DHostTest : public testing::Test { 27 public: 28 static bool ConvertToLogicalPixels(float scale, 29 gfx::Rect* op_rect, 30 gfx::Point* delta) { 31 return PepperGraphics2DHost::ConvertToLogicalPixels(scale, op_rect, delta); 32 } 33 34 PepperGraphics2DHostTest() : renderer_ppapi_host_(NULL, 12345) {} 35 36 virtual ~PepperGraphics2DHostTest() { 37 ppapi::ProxyAutoLock proxy_lock; 38 host_.reset(); 39 } 40 41 void Init(PP_Instance instance, 42 const PP_Size& backing_store_size, 43 const PP_Rect& plugin_rect) { 44 renderer_view_data_.rect = plugin_rect; 45 test_globals_.GetResourceTracker()->DidCreateInstance(instance); 46 scoped_refptr<PPB_ImageData_Impl> backing_store( 47 new PPB_ImageData_Impl(instance, PPB_ImageData_Impl::ForTest())); 48 host_.reset(PepperGraphics2DHost::Create(&renderer_ppapi_host_, 49 instance, 50 12345, 51 backing_store_size, 52 PP_FALSE, 53 backing_store)); 54 DCHECK(host_.get()); 55 } 56 57 void PaintImageData(PPB_ImageData_Impl* image_data) { 58 ppapi::HostResource image_data_resource; 59 image_data_resource.SetHostResource(image_data->pp_instance(), 60 image_data->pp_resource()); 61 host_->OnHostMsgPaintImageData( 62 NULL, image_data_resource, PP_Point(), false, PP_Rect()); 63 } 64 65 void Flush() { 66 ppapi::host::HostMessageContext context( 67 ppapi::proxy::ResourceMessageCallParams(host_->pp_resource(), 0)); 68 std::vector<ui::LatencyInfo> latency; 69 host_->OnHostMsgFlush(&context, latency); 70 host_->ViewFlushedPaint(); 71 host_->SendOffscreenFlushAck(); 72 } 73 74 void PaintToWebCanvas(SkBitmap* bitmap) { 75 scoped_ptr<WebCanvas> canvas(new WebCanvas(*bitmap)); 76 gfx::Rect plugin_rect(PP_ToGfxRect(renderer_view_data_.rect)); 77 host_->Paint(canvas.get(), 78 plugin_rect, 79 gfx::Rect(0, 0, plugin_rect.width(), plugin_rect.height())); 80 } 81 82 void ResetPageBitmap(SkBitmap* bitmap) { 83 PP_Rect plugin_rect = renderer_view_data_.rect; 84 int width = plugin_rect.point.x + plugin_rect.size.width; 85 int height = plugin_rect.point.y + plugin_rect.size.height; 86 if (bitmap->isNull() || bitmap->width() != width || 87 bitmap->height() != height) { 88 bitmap->setConfig(SkBitmap::kARGB_8888_Config, width, height); 89 bitmap->allocPixels(); 90 } 91 bitmap->eraseColor(0); 92 } 93 94 private: 95 ppapi::ViewData renderer_view_data_; 96 scoped_ptr<PepperGraphics2DHost> host_; 97 base::MessageLoop message_loop_; 98 MockRendererPpapiHost renderer_ppapi_host_; 99 ppapi::TestGlobals test_globals_; 100 }; 101 102 TEST_F(PepperGraphics2DHostTest, ConvertToLogicalPixels) { 103 static const struct { 104 int x1; 105 int y1; 106 int w1; 107 int h1; 108 int x2; 109 int y2; 110 int w2; 111 int h2; 112 int dx1; 113 int dy1; 114 int dx2; 115 int dy2; 116 float scale; 117 bool result; 118 } tests[] = {{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1.0, true}, 119 {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 2.0, true}, 120 {0, 0, 4, 4, 0, 0, 2, 2, 0, 0, 0, 0, 0.5, true}, 121 {1, 1, 4, 4, 0, 0, 3, 3, 0, 0, 0, 0, 0.5, false}, 122 {53, 75, 100, 100, 53, 75, 100, 100, 0, 0, 0, 0, 1.0, true}, 123 {53, 75, 100, 100, 106, 150, 200, 200, 0, 0, 0, 0, 2.0, true}, 124 {53, 75, 100, 100, 26, 37, 51, 51, 0, 0, 0, 0, 0.5, false}, 125 {53, 74, 100, 100, 26, 37, 51, 50, 0, 0, 0, 0, 0.5, false}, 126 {-1, -1, 100, 100, -1, -1, 51, 51, 0, 0, 0, 0, 0.5, false}, 127 {-2, -2, 100, 100, -1, -1, 50, 50, 4, -4, 2, -2, 0.5, true}, 128 {-101, -100, 50, 50, -51, -50, 26, 25, 0, 0, 0, 0, 0.5, false}, 129 {10, 10, 20, 20, 5, 5, 10, 10, 0, 0, 0, 0, 0.5, true}, 130 // Cannot scroll due to partial coverage on sides 131 {11, 10, 20, 20, 5, 5, 11, 10, 0, 0, 0, 0, 0.5, false}, 132 // Can scroll since backing store is actually smaller/scaling up 133 {11, 20, 100, 100, 22, 40, 200, 200, 7, 3, 14, 6, 2.0, true}, 134 // Can scroll due to delta and bounds being aligned 135 {10, 10, 20, 20, 5, 5, 10, 10, 6, 4, 3, 2, 0.5, true}, 136 // Cannot scroll due to dx 137 {10, 10, 20, 20, 5, 5, 10, 10, 5, 4, 2, 2, 0.5, false}, 138 // Cannot scroll due to dy 139 {10, 10, 20, 20, 5, 5, 10, 10, 6, 3, 3, 1, 0.5, false}, 140 // Cannot scroll due to top 141 {10, 11, 20, 20, 5, 5, 10, 11, 6, 4, 3, 2, 0.5, false}, 142 // Cannot scroll due to left 143 {7, 10, 20, 20, 3, 5, 11, 10, 6, 4, 3, 2, 0.5, false}, 144 // Cannot scroll due to width 145 {10, 10, 21, 20, 5, 5, 11, 10, 6, 4, 3, 2, 0.5, false}, 146 // Cannot scroll due to height 147 {10, 10, 20, 51, 5, 5, 10, 26, 6, 4, 3, 2, 0.5, false}, 148 // Check negative scroll deltas 149 {10, 10, 20, 20, 5, 5, 10, 10, -6, -4, -3, -2, 0.5, true}, 150 {10, 10, 20, 20, 5, 5, 10, 10, -6, -3, -3, -1, 0.5, false}, }; 151 for (size_t i = 0; i < ARRAYSIZE_UNSAFE(tests); ++i) { 152 gfx::Rect r1(tests[i].x1, tests[i].y1, tests[i].w1, tests[i].h1); 153 gfx::Rect r2(tests[i].x2, tests[i].y2, tests[i].w2, tests[i].h2); 154 gfx::Rect orig = r1; 155 gfx::Point delta(tests[i].dx1, tests[i].dy1); 156 bool res = ConvertToLogicalPixels(tests[i].scale, &r1, &delta); 157 EXPECT_EQ(r2.ToString(), r1.ToString()); 158 EXPECT_EQ(res, tests[i].result); 159 if (res) { 160 EXPECT_EQ(delta, gfx::Point(tests[i].dx2, tests[i].dy2)); 161 } 162 // Reverse the scale and ensure all the original pixels are still inside 163 // the result. 164 ConvertToLogicalPixels(1.0f / tests[i].scale, &r1, NULL); 165 EXPECT_TRUE(r1.Contains(orig)); 166 } 167 } 168 169 } // namespace content 170