Home | History | Annotate | Download | only in ext
      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 // TODO(awalker): clean up the const/non-const reference handling in this test
      6 
      7 #include "build/build_config.h"
      8 
      9 #if defined(OS_MACOSX)
     10 #import <ApplicationServices/ApplicationServices.h>
     11 #endif
     12 
     13 #if !defined(OS_WIN)
     14 #include <unistd.h>
     15 #endif
     16 
     17 #include "base/memory/scoped_ptr.h"
     18 #include "skia/ext/platform_canvas.h"
     19 #include "skia/ext/platform_device.h"
     20 #include "testing/gtest/include/gtest/gtest.h"
     21 #include "third_party/skia/include/core/SkBitmap.h"
     22 #include "third_party/skia/include/core/SkColor.h"
     23 #include "third_party/skia/include/core/SkPixelRef.h"
     24 
     25 namespace skia {
     26 
     27 namespace {
     28 
     29 // Return true if the canvas is filled to canvas_color, and contains a single
     30 // rectangle filled to rect_color. This function ignores the alpha channel,
     31 // since Windows will sometimes clear the alpha channel when drawing, and we
     32 // will fix that up later in cases it's necessary.
     33 bool VerifyRect(const PlatformCanvas& canvas,
     34                 uint32_t canvas_color, uint32_t rect_color,
     35                 int x, int y, int w, int h) {
     36   SkBaseDevice* device = skia::GetTopDevice(canvas);
     37   const SkBitmap& bitmap = device->accessBitmap(false);
     38   SkAutoLockPixels lock(bitmap);
     39 
     40   // For masking out the alpha values.
     41   uint32_t alpha_mask = 0xFF << SK_A32_SHIFT;
     42 
     43   for (int cur_y = 0; cur_y < bitmap.height(); cur_y++) {
     44     for (int cur_x = 0; cur_x < bitmap.width(); cur_x++) {
     45       if (cur_x >= x && cur_x < x + w &&
     46           cur_y >= y && cur_y < y + h) {
     47         // Inside the square should be rect_color
     48         if ((*bitmap.getAddr32(cur_x, cur_y) | alpha_mask) !=
     49             (rect_color | alpha_mask))
     50           return false;
     51       } else {
     52         // Outside the square should be canvas_color
     53         if ((*bitmap.getAddr32(cur_x, cur_y) | alpha_mask) !=
     54             (canvas_color | alpha_mask))
     55           return false;
     56       }
     57     }
     58   }
     59   return true;
     60 }
     61 
     62 #if !defined(OS_MACOSX)
     63 bool IsOfColor(const SkBitmap& bitmap, int x, int y, uint32_t color) {
     64   // For masking out the alpha values.
     65   static uint32_t alpha_mask = 0xFF << SK_A32_SHIFT;
     66   return (*bitmap.getAddr32(x, y) | alpha_mask) == (color | alpha_mask);
     67 }
     68 
     69 // Return true if canvas has something that passes for a rounded-corner
     70 // rectangle. Basically, we're just checking to make sure that the pixels in the
     71 // middle are of rect_color and pixels in the corners are of canvas_color.
     72 bool VerifyRoundedRect(const PlatformCanvas& canvas,
     73                        uint32_t canvas_color, uint32_t rect_color,
     74                        int x, int y, int w, int h) {
     75   SkBaseDevice* device = skia::GetTopDevice(canvas);
     76   const SkBitmap& bitmap = device->accessBitmap(false);
     77   SkAutoLockPixels lock(bitmap);
     78 
     79   // Check corner points first. They should be of canvas_color.
     80   if (!IsOfColor(bitmap, x, y, canvas_color)) return false;
     81   if (!IsOfColor(bitmap, x + w, y, canvas_color)) return false;
     82   if (!IsOfColor(bitmap, x, y + h, canvas_color)) return false;
     83   if (!IsOfColor(bitmap, x + w, y, canvas_color)) return false;
     84 
     85   // Check middle points. They should be of rect_color.
     86   if (!IsOfColor(bitmap, (x + w / 2), y, rect_color)) return false;
     87   if (!IsOfColor(bitmap, x, (y + h / 2), rect_color)) return false;
     88   if (!IsOfColor(bitmap, x + w, (y + h / 2), rect_color)) return false;
     89   if (!IsOfColor(bitmap, (x + w / 2), y + h, rect_color)) return false;
     90 
     91   return true;
     92 }
     93 #endif
     94 
     95 // Checks whether there is a white canvas with a black square at the given
     96 // location in pixels (not in the canvas coordinate system).
     97 bool VerifyBlackRect(const PlatformCanvas& canvas, int x, int y, int w, int h) {
     98   return VerifyRect(canvas, SK_ColorWHITE, SK_ColorBLACK, x, y, w, h);
     99 }
    100 
    101 // Check that every pixel in the canvas is a single color.
    102 bool VerifyCanvasColor(const PlatformCanvas& canvas, uint32_t canvas_color) {
    103   return VerifyRect(canvas, canvas_color, 0, 0, 0, 0, 0);
    104 }
    105 
    106 #if defined(OS_WIN)
    107 void DrawNativeRect(PlatformCanvas& canvas, int x, int y, int w, int h) {
    108   skia::ScopedPlatformPaint scoped_platform_paint(&canvas);
    109   HDC dc = scoped_platform_paint.GetPlatformSurface();
    110 
    111   RECT inner_rc;
    112   inner_rc.left = x;
    113   inner_rc.top = y;
    114   inner_rc.right = x + w;
    115   inner_rc.bottom = y + h;
    116   FillRect(dc, &inner_rc, reinterpret_cast<HBRUSH>(GetStockObject(BLACK_BRUSH)));
    117 }
    118 #elif defined(OS_MACOSX)
    119 void DrawNativeRect(PlatformCanvas& canvas, int x, int y, int w, int h) {
    120   skia::ScopedPlatformPaint scoped_platform_paint(&canvas);
    121   CGContextRef context = scoped_platform_paint.GetPlatformSurface();
    122 
    123   CGRect inner_rc = CGRectMake(x, y, w, h);
    124   // RGBA opaque black
    125   CGColorRef black = CGColorCreateGenericRGB(0.0, 0.0, 0.0, 1.0);
    126   CGContextSetFillColorWithColor(context, black);
    127   CGColorRelease(black);
    128   CGContextFillRect(context, inner_rc);
    129 }
    130 #else
    131 void DrawNativeRect(PlatformCanvas& canvas, int x, int y, int w, int h) {
    132   notImplemented();
    133 }
    134 #endif
    135 
    136 // Clips the contents of the canvas to the given rectangle. This will be
    137 // intersected with any existing clip.
    138 void AddClip(PlatformCanvas& canvas, int x, int y, int w, int h) {
    139   SkRect rect;
    140   rect.set(SkIntToScalar(x), SkIntToScalar(y),
    141            SkIntToScalar(x + w), SkIntToScalar(y + h));
    142   canvas.clipRect(rect);
    143 }
    144 
    145 class LayerSaver {
    146  public:
    147   LayerSaver(PlatformCanvas& canvas, int x, int y, int w, int h)
    148       : canvas_(canvas),
    149         x_(x),
    150         y_(y),
    151         w_(w),
    152         h_(h) {
    153     SkRect bounds;
    154     bounds.set(SkIntToScalar(x_), SkIntToScalar(y_),
    155                SkIntToScalar(right()), SkIntToScalar(bottom()));
    156     canvas_.saveLayer(&bounds, NULL);
    157     canvas.clear(SkColorSetARGB(0, 0, 0, 0));
    158   }
    159 
    160   ~LayerSaver() {
    161     canvas_.restore();
    162   }
    163 
    164   int x() const { return x_; }
    165   int y() const { return y_; }
    166   int w() const { return w_; }
    167   int h() const { return h_; }
    168 
    169   // Returns the EXCLUSIVE far bounds of the layer.
    170   int right() const { return x_ + w_; }
    171   int bottom() const { return y_ + h_; }
    172 
    173  private:
    174   PlatformCanvas& canvas_;
    175   int x_, y_, w_, h_;
    176 };
    177 
    178 // Size used for making layers in many of the below tests.
    179 const int kLayerX = 2;
    180 const int kLayerY = 3;
    181 const int kLayerW = 9;
    182 const int kLayerH = 7;
    183 
    184 // Size used by some tests to draw a rectangle inside the layer.
    185 const int kInnerX = 4;
    186 const int kInnerY = 5;
    187 const int kInnerW = 2;
    188 const int kInnerH = 3;
    189 
    190 }
    191 
    192 // This just checks that our checking code is working properly, it just uses
    193 // regular skia primitives.
    194 TEST(PlatformCanvas, SkLayer) {
    195   // Create the canvas initialized to opaque white.
    196   RefPtr<SkCanvas> canvas = AdoptRef(CreatePlatformCanvas(16, 16, true));
    197   canvas->drawColor(SK_ColorWHITE);
    198 
    199   // Make a layer and fill it completely to make sure that the bounds are
    200   // correct.
    201   {
    202     LayerSaver layer(*canvas, kLayerX, kLayerY, kLayerW, kLayerH);
    203     canvas->drawColor(SK_ColorBLACK);
    204   }
    205   EXPECT_TRUE(VerifyBlackRect(*canvas, kLayerX, kLayerY, kLayerW, kLayerH));
    206 }
    207 
    208 #if !defined(USE_AURA)  // http://crbug.com/154358
    209 
    210 // Test native clipping.
    211 TEST(PlatformCanvas, ClipRegion) {
    212   // Initialize a white canvas
    213   RefPtr<SkCanvas> canvas = AdoptRef(CreatePlatformCanvas(16, 16, true));
    214   canvas->drawColor(SK_ColorWHITE);
    215   EXPECT_TRUE(VerifyCanvasColor(*canvas, SK_ColorWHITE));
    216 
    217   // Test that initially the canvas has no clip region, by filling it
    218   // with a black rectangle.
    219   // Note: Don't use LayerSaver, since internally it sets a clip region.
    220   DrawNativeRect(*canvas, 0, 0, 16, 16);
    221   EXPECT_TRUE(VerifyCanvasColor(*canvas, SK_ColorBLACK));
    222 
    223   // Test that intersecting disjoint clip rectangles sets an empty clip region
    224   canvas->drawColor(SK_ColorWHITE);
    225   EXPECT_TRUE(VerifyCanvasColor(*canvas, SK_ColorWHITE));
    226   {
    227     LayerSaver layer(*canvas, 0, 0, 16, 16);
    228     AddClip(*canvas, 2, 3, 4, 5);
    229     AddClip(*canvas, 4, 9, 10, 10);
    230     DrawNativeRect(*canvas, 0, 0, 16, 16);
    231   }
    232   EXPECT_TRUE(VerifyCanvasColor(*canvas, SK_ColorWHITE));
    233 }
    234 
    235 #endif  // !defined(USE_AURA)
    236 
    237 // Test the layers get filled properly by native rendering.
    238 TEST(PlatformCanvas, FillLayer) {
    239   // Create the canvas initialized to opaque white.
    240   RefPtr<SkCanvas> canvas = AdoptRef(CreatePlatformCanvas(16, 16, true));
    241 
    242   // Make a layer and fill it completely to make sure that the bounds are
    243   // correct.
    244   canvas->drawColor(SK_ColorWHITE);
    245   {
    246     LayerSaver layer(*canvas, kLayerX, kLayerY, kLayerW, kLayerH);
    247     DrawNativeRect(*canvas, 0, 0, 100, 100);
    248 #if defined(OS_WIN)
    249     MakeOpaque(canvas.get(), 0, 0, 100, 100);
    250 #endif
    251   }
    252   EXPECT_TRUE(VerifyBlackRect(*canvas, kLayerX, kLayerY, kLayerW, kLayerH));
    253 
    254   // Make a layer and fill it partially to make sure the translation is correct.
    255   canvas->drawColor(SK_ColorWHITE);
    256   {
    257     LayerSaver layer(*canvas, kLayerX, kLayerY, kLayerW, kLayerH);
    258     DrawNativeRect(*canvas, kInnerX, kInnerY, kInnerW, kInnerH);
    259 #if defined(OS_WIN)
    260     MakeOpaque(canvas.get(), kInnerX, kInnerY, kInnerW, kInnerH);
    261 #endif
    262   }
    263   EXPECT_TRUE(VerifyBlackRect(*canvas, kInnerX, kInnerY, kInnerW, kInnerH));
    264 
    265   // Add a clip on the layer and fill to make sure clip is correct.
    266   canvas->drawColor(SK_ColorWHITE);
    267   {
    268     LayerSaver layer(*canvas, kLayerX, kLayerY, kLayerW, kLayerH);
    269     canvas->save();
    270     AddClip(*canvas, kInnerX, kInnerY, kInnerW, kInnerH);
    271     DrawNativeRect(*canvas, 0, 0, 100, 100);
    272 #if defined(OS_WIN)
    273     MakeOpaque(canvas.get(), kInnerX, kInnerY, kInnerW, kInnerH);
    274 #endif
    275     canvas->restore();
    276   }
    277   EXPECT_TRUE(VerifyBlackRect(*canvas, kInnerX, kInnerY, kInnerW, kInnerH));
    278 
    279   // Add a clip and then make the layer to make sure the clip is correct.
    280   canvas->drawColor(SK_ColorWHITE);
    281   canvas->save();
    282   AddClip(*canvas, kInnerX, kInnerY, kInnerW, kInnerH);
    283   {
    284     LayerSaver layer(*canvas, kLayerX, kLayerY, kLayerW, kLayerH);
    285     DrawNativeRect(*canvas, 0, 0, 100, 100);
    286 #if defined(OS_WIN)
    287     MakeOpaque(canvas.get(), 0, 0, 100, 100);
    288 #endif
    289   }
    290   canvas->restore();
    291   EXPECT_TRUE(VerifyBlackRect(*canvas, kInnerX, kInnerY, kInnerW, kInnerH));
    292 }
    293 
    294 #if !defined(USE_AURA)  // http://crbug.com/154358
    295 
    296 // Test that translation + make layer works properly.
    297 TEST(PlatformCanvas, TranslateLayer) {
    298   // Create the canvas initialized to opaque white.
    299   RefPtr<SkCanvas> canvas = AdoptRef(CreatePlatformCanvas(16, 16, true));
    300 
    301   // Make a layer and fill it completely to make sure that the bounds are
    302   // correct.
    303   canvas->drawColor(SK_ColorWHITE);
    304   canvas->save();
    305   canvas->translate(1, 1);
    306   {
    307     LayerSaver layer(*canvas, kLayerX, kLayerY, kLayerW, kLayerH);
    308     DrawNativeRect(*canvas, 0, 0, 100, 100);
    309 #if defined(OS_WIN)
    310     MakeOpaque(canvas.get(), 0, 0, 100, 100);
    311 #endif
    312   }
    313   canvas->restore();
    314   EXPECT_TRUE(VerifyBlackRect(*canvas, kLayerX + 1, kLayerY + 1,
    315                               kLayerW, kLayerH));
    316 
    317   // Translate then make the layer.
    318   canvas->drawColor(SK_ColorWHITE);
    319   canvas->save();
    320   canvas->translate(1, 1);
    321   {
    322     LayerSaver layer(*canvas, kLayerX, kLayerY, kLayerW, kLayerH);
    323     DrawNativeRect(*canvas, kInnerX, kInnerY, kInnerW, kInnerH);
    324 #if defined(OS_WIN)
    325     MakeOpaque(canvas.get(), kInnerX, kInnerY, kInnerW, kInnerH);
    326 #endif
    327   }
    328   canvas->restore();
    329   EXPECT_TRUE(VerifyBlackRect(*canvas, kInnerX + 1, kInnerY + 1,
    330                               kInnerW, kInnerH));
    331 
    332   // Make the layer then translate.
    333   canvas->drawColor(SK_ColorWHITE);
    334   canvas->save();
    335   {
    336     LayerSaver layer(*canvas, kLayerX, kLayerY, kLayerW, kLayerH);
    337     canvas->translate(1, 1);
    338     DrawNativeRect(*canvas, kInnerX, kInnerY, kInnerW, kInnerH);
    339 #if defined(OS_WIN)
    340     MakeOpaque(canvas.get(), kInnerX, kInnerY, kInnerW, kInnerH);
    341 #endif
    342   }
    343   canvas->restore();
    344   EXPECT_TRUE(VerifyBlackRect(*canvas, kInnerX + 1, kInnerY + 1,
    345                               kInnerW, kInnerH));
    346 
    347   // Translate both before and after, and have a clip.
    348   canvas->drawColor(SK_ColorWHITE);
    349   canvas->save();
    350   canvas->translate(1, 1);
    351   {
    352     LayerSaver layer(*canvas, kLayerX, kLayerY, kLayerW, kLayerH);
    353     canvas->drawColor(SK_ColorWHITE);
    354     canvas->translate(1, 1);
    355     AddClip(*canvas, kInnerX + 1, kInnerY + 1, kInnerW - 1, kInnerH - 1);
    356     DrawNativeRect(*canvas, 0, 0, 100, 100);
    357 #if defined(OS_WIN)
    358     MakeOpaque(canvas.get(), kLayerX, kLayerY, kLayerW, kLayerH);
    359 #endif
    360   }
    361   canvas->restore();
    362   EXPECT_TRUE(VerifyBlackRect(*canvas, kInnerX + 3, kInnerY + 3,
    363                               kInnerW - 1, kInnerH - 1));
    364 
    365 // TODO(dglazkov): Figure out why this fails on Mac (antialiased clipping?),
    366 // modify test and remove this guard.
    367 #if !defined(OS_MACOSX)
    368   // Translate both before and after, and have a path clip.
    369   canvas->drawColor(SK_ColorWHITE);
    370   canvas->save();
    371   canvas->translate(1, 1);
    372   {
    373     LayerSaver layer(*canvas, kLayerX, kLayerY, kLayerW, kLayerH);
    374     canvas->drawColor(SK_ColorWHITE);
    375     canvas->translate(1, 1);
    376 
    377     SkPath path;
    378     SkRect rect;
    379     rect.iset(kInnerX - 1, kInnerY - 1,
    380               kInnerX + kInnerW, kInnerY + kInnerH);
    381     const SkScalar kRadius = 2.0;
    382     path.addRoundRect(rect, kRadius, kRadius);
    383     canvas->clipPath(path);
    384 
    385     DrawNativeRect(*canvas, 0, 0, 100, 100);
    386 #if defined(OS_WIN)
    387     MakeOpaque(canvas.get(), kLayerX, kLayerY, kLayerW, kLayerH);
    388 #endif
    389   }
    390   canvas->restore();
    391   EXPECT_TRUE(VerifyRoundedRect(*canvas, SK_ColorWHITE, SK_ColorBLACK,
    392                                 kInnerX + 1, kInnerY + 1, kInnerW, kInnerH));
    393 #endif
    394 }
    395 
    396 #endif  // #if !defined(USE_AURA)
    397 
    398 TEST(PlatformBitmapTest, PlatformBitmap) {
    399   const int kWidth = 400;
    400   const int kHeight = 300;
    401   scoped_ptr<PlatformBitmap> platform_bitmap(new PlatformBitmap);
    402 
    403   EXPECT_TRUE(0 == platform_bitmap->GetSurface());
    404   EXPECT_TRUE(platform_bitmap->GetBitmap().empty());
    405   EXPECT_TRUE(platform_bitmap->GetBitmap().isNull());
    406 
    407   EXPECT_TRUE(platform_bitmap->Allocate(kWidth, kHeight, /*is_opaque=*/false));
    408 
    409   EXPECT_TRUE(0 != platform_bitmap->GetSurface());
    410   EXPECT_FALSE(platform_bitmap->GetBitmap().empty());
    411   EXPECT_FALSE(platform_bitmap->GetBitmap().isNull());
    412   EXPECT_EQ(kWidth, platform_bitmap->GetBitmap().width());
    413   EXPECT_EQ(kHeight, platform_bitmap->GetBitmap().height());
    414   EXPECT_LE(static_cast<size_t>(platform_bitmap->GetBitmap().width()*4),
    415             platform_bitmap->GetBitmap().rowBytes());
    416   EXPECT_EQ(SkBitmap::kARGB_8888_Config,  // Same for all platforms.
    417             platform_bitmap->GetBitmap().config());
    418   EXPECT_TRUE(platform_bitmap->GetBitmap().lockPixelsAreWritable());
    419   EXPECT_TRUE(platform_bitmap->GetBitmap().pixelRef()->isLocked());
    420   EXPECT_EQ(1, platform_bitmap->GetBitmap().pixelRef()->getRefCnt());
    421 
    422   *(platform_bitmap->GetBitmap().getAddr32(10, 20)) = 0xDEED1020;
    423   *(platform_bitmap->GetBitmap().getAddr32(20, 30)) = 0xDEED2030;
    424 
    425   SkBitmap sk_bitmap = platform_bitmap->GetBitmap();
    426   sk_bitmap.lockPixels();
    427 
    428   EXPECT_EQ(2, platform_bitmap->GetBitmap().pixelRef()->getRefCnt());
    429   EXPECT_EQ(2, sk_bitmap.pixelRef()->getRefCnt());
    430 
    431   EXPECT_EQ(0xDEED1020, *sk_bitmap.getAddr32(10, 20));
    432   EXPECT_EQ(0xDEED2030, *sk_bitmap.getAddr32(20, 30));
    433 
    434   *(platform_bitmap->GetBitmap().getAddr32(30, 40)) = 0xDEED3040;
    435 
    436   // The SkBitmaps derived from a PlatformBitmap must be capable of outliving
    437   // the PlatformBitmap.
    438   platform_bitmap.reset();
    439 
    440   EXPECT_EQ(1, sk_bitmap.pixelRef()->getRefCnt());
    441 
    442   EXPECT_EQ(0xDEED1020, *sk_bitmap.getAddr32(10, 20));
    443   EXPECT_EQ(0xDEED2030, *sk_bitmap.getAddr32(20, 30));
    444   EXPECT_EQ(0xDEED3040, *sk_bitmap.getAddr32(30, 40));
    445   sk_bitmap.unlockPixels();
    446 
    447   EXPECT_EQ(NULL, sk_bitmap.getPixels());
    448 
    449   sk_bitmap.lockPixels();
    450   EXPECT_EQ(0xDEED1020, *sk_bitmap.getAddr32(10, 20));
    451   EXPECT_EQ(0xDEED2030, *sk_bitmap.getAddr32(20, 30));
    452   EXPECT_EQ(0xDEED3040, *sk_bitmap.getAddr32(30, 40));
    453   sk_bitmap.unlockPixels();
    454 }
    455 
    456 
    457 }  // namespace skia
    458