Home | History | Annotate | Download | only in gfx
      1 // Copyright (c) 2010 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 "base/basictypes.h"
      6 #include "base/memory/shared_memory.h"
      7 #include "skia/ext/platform_canvas.h"
      8 #include "testing/gtest/include/gtest/gtest.h"
      9 #include "ui/gfx/blit.h"
     10 #include "ui/gfx/point.h"
     11 #include "ui/gfx/rect.h"
     12 
     13 namespace {
     14 
     15 // Fills the given canvas with the values by duplicating the values into each
     16 // color channel for the corresponding pixel.
     17 //
     18 // Example values = {{0x0, 0x01}, {0x12, 0xFF}} would give a canvas with:
     19 //   0x00000000 0x01010101
     20 //   0x12121212 0xFFFFFFFF
     21 template<int w, int h>
     22 void SetToCanvas(skia::PlatformCanvas* canvas, uint8 values[h][w]) {
     23   SkBitmap& bitmap = const_cast<SkBitmap&>(
     24       skia::GetTopDevice(*canvas)->accessBitmap(true));
     25   SkAutoLockPixels lock(bitmap);
     26   ASSERT_EQ(w, bitmap.width());
     27   ASSERT_EQ(h, bitmap.height());
     28 
     29   for (int y = 0; y < h; y++) {
     30     for (int x = 0; x < w; x++) {
     31       uint8 value = values[y][x];
     32       *bitmap.getAddr32(x, y) =
     33           (value << 24) | (value << 16) | (value << 8) | value;
     34     }
     35   }
     36 }
     37 
     38 // Checks each pixel in the given canvas and see if it is made up of the given
     39 // values, where each value has been duplicated into each channel of the given
     40 // bitmap (see SetToCanvas above).
     41 template<int w, int h>
     42 void VerifyCanvasValues(skia::PlatformCanvas* canvas, uint8 values[h][w]) {
     43   SkBitmap& bitmap = const_cast<SkBitmap&>(
     44       skia::GetTopDevice(*canvas)->accessBitmap(true));
     45   SkAutoLockPixels lock(bitmap);
     46   ASSERT_EQ(w, bitmap.width());
     47   ASSERT_EQ(h, bitmap.height());
     48 
     49   for (int y = 0; y < h; y++) {
     50     for (int x = 0; x < w; x++) {
     51       uint8 value = values[y][x];
     52       uint32 expected =
     53           (value << 24) | (value << 16) | (value << 8) | value;
     54       ASSERT_EQ(expected, *bitmap.getAddr32(x, y));
     55     }
     56   }
     57 }
     58 
     59 }  // namespace
     60 
     61 TEST(Blit, ScrollCanvas) {
     62   static const int kCanvasWidth = 5;
     63   static const int kCanvasHeight = 5;
     64   skia::RefPtr<SkCanvas> canvas = skia::AdoptRef(
     65       skia::CreatePlatformCanvas(kCanvasWidth, kCanvasHeight, true));
     66   uint8 initial_values[kCanvasHeight][kCanvasWidth] = {
     67       { 0x00, 0x01, 0x02, 0x03, 0x04 },
     68       { 0x10, 0x11, 0x12, 0x13, 0x14 },
     69       { 0x20, 0x21, 0x22, 0x23, 0x24 },
     70       { 0x30, 0x31, 0x32, 0x33, 0x34 },
     71       { 0x40, 0x41, 0x42, 0x43, 0x44 }};
     72   SetToCanvas<5, 5>(canvas.get(), initial_values);
     73 
     74   // Sanity check on input.
     75   VerifyCanvasValues<5, 5>(canvas.get(), initial_values);
     76 
     77   // Scroll none and make sure it's a NOP.
     78   gfx::ScrollCanvas(canvas.get(),
     79                     gfx::Rect(0, 0, kCanvasWidth, kCanvasHeight),
     80                     gfx::Vector2d(0, 0));
     81   VerifyCanvasValues<5, 5>(canvas.get(), initial_values);
     82 
     83   // Scroll the center 3 pixels up one.
     84   gfx::Rect center_three(1, 1, 3, 3);
     85   gfx::ScrollCanvas(canvas.get(), center_three, gfx::Vector2d(0, -1));
     86   uint8 scroll_up_expected[kCanvasHeight][kCanvasWidth] = {
     87       { 0x00, 0x01, 0x02, 0x03, 0x04 },
     88       { 0x10, 0x21, 0x22, 0x23, 0x14 },
     89       { 0x20, 0x31, 0x32, 0x33, 0x24 },
     90       { 0x30, 0x31, 0x32, 0x33, 0x34 },
     91       { 0x40, 0x41, 0x42, 0x43, 0x44 }};
     92   VerifyCanvasValues<5, 5>(canvas.get(), scroll_up_expected);
     93 
     94   // Reset and scroll the center 3 pixels down one.
     95   SetToCanvas<5, 5>(canvas.get(), initial_values);
     96   gfx::ScrollCanvas(canvas.get(), center_three, gfx::Vector2d(0, 1));
     97   uint8 scroll_down_expected[kCanvasHeight][kCanvasWidth] = {
     98       { 0x00, 0x01, 0x02, 0x03, 0x04 },
     99       { 0x10, 0x11, 0x12, 0x13, 0x14 },
    100       { 0x20, 0x11, 0x12, 0x13, 0x24 },
    101       { 0x30, 0x21, 0x22, 0x23, 0x34 },
    102       { 0x40, 0x41, 0x42, 0x43, 0x44 }};
    103   VerifyCanvasValues<5, 5>(canvas.get(), scroll_down_expected);
    104 
    105   // Reset and scroll the center 3 pixels right one.
    106   SetToCanvas<5, 5>(canvas.get(), initial_values);
    107   gfx::ScrollCanvas(canvas.get(), center_three, gfx::Vector2d(1, 0));
    108   uint8 scroll_right_expected[kCanvasHeight][kCanvasWidth] = {
    109       { 0x00, 0x01, 0x02, 0x03, 0x04 },
    110       { 0x10, 0x11, 0x11, 0x12, 0x14 },
    111       { 0x20, 0x21, 0x21, 0x22, 0x24 },
    112       { 0x30, 0x31, 0x31, 0x32, 0x34 },
    113       { 0x40, 0x41, 0x42, 0x43, 0x44 }};
    114   VerifyCanvasValues<5, 5>(canvas.get(), scroll_right_expected);
    115 
    116   // Reset and scroll the center 3 pixels left one.
    117   SetToCanvas<5, 5>(canvas.get(), initial_values);
    118   gfx::ScrollCanvas(canvas.get(), center_three, gfx::Vector2d(-1, 0));
    119   uint8 scroll_left_expected[kCanvasHeight][kCanvasWidth] = {
    120       { 0x00, 0x01, 0x02, 0x03, 0x04 },
    121       { 0x10, 0x12, 0x13, 0x13, 0x14 },
    122       { 0x20, 0x22, 0x23, 0x23, 0x24 },
    123       { 0x30, 0x32, 0x33, 0x33, 0x34 },
    124       { 0x40, 0x41, 0x42, 0x43, 0x44 }};
    125   VerifyCanvasValues<5, 5>(canvas.get(), scroll_left_expected);
    126 
    127   // Diagonal scroll.
    128   SetToCanvas<5, 5>(canvas.get(), initial_values);
    129   gfx::ScrollCanvas(canvas.get(), center_three, gfx::Vector2d(2, 2));
    130   uint8 scroll_diagonal_expected[kCanvasHeight][kCanvasWidth] = {
    131       { 0x00, 0x01, 0x02, 0x03, 0x04 },
    132       { 0x10, 0x11, 0x12, 0x13, 0x14 },
    133       { 0x20, 0x21, 0x22, 0x23, 0x24 },
    134       { 0x30, 0x31, 0x32, 0x11, 0x34 },
    135       { 0x40, 0x41, 0x42, 0x43, 0x44 }};
    136   VerifyCanvasValues<5, 5>(canvas.get(), scroll_diagonal_expected);
    137 }
    138 
    139 #if defined(OS_WIN)
    140 
    141 TEST(Blit, WithSharedMemory) {
    142   const int kCanvasWidth = 5;
    143   const int kCanvasHeight = 5;
    144   base::SharedMemory shared_mem;
    145   ASSERT_TRUE(shared_mem.CreateAnonymous(kCanvasWidth * kCanvasHeight));
    146   base::SharedMemoryHandle section = shared_mem.handle();
    147   skia::RefPtr<SkCanvas> canvas = skia::AdoptRef(
    148       skia::CreatePlatformCanvas(kCanvasWidth, kCanvasHeight, true, section,
    149                                  skia::RETURN_NULL_ON_FAILURE));
    150   ASSERT_TRUE(canvas);
    151   shared_mem.Close();
    152 
    153   uint8 initial_values[kCanvasHeight][kCanvasWidth] = {
    154       { 0x00, 0x01, 0x02, 0x03, 0x04 },
    155       { 0x10, 0x11, 0x12, 0x13, 0x14 },
    156       { 0x20, 0x21, 0x22, 0x23, 0x24 },
    157       { 0x30, 0x31, 0x32, 0x33, 0x34 },
    158       { 0x40, 0x41, 0x42, 0x43, 0x44 }};
    159   SetToCanvas<5, 5>(canvas.get(), initial_values);
    160 
    161   // Sanity check on input.
    162   VerifyCanvasValues<5, 5>(canvas.get(), initial_values);
    163 }
    164 
    165 #endif
    166 
    167