summaryrefslogtreecommitdiffstats
path: root/gfx
diff options
context:
space:
mode:
authorbrettw@chromium.org <brettw@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98>2010-07-15 04:33:51 +0000
committerbrettw@chromium.org <brettw@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98>2010-07-15 04:33:51 +0000
commita83476617999697763c3c02115d3d0f7ecfe050f (patch)
tree8a9ed3a54b3fa5db28dd894413d79b3850bb909a /gfx
parent2c79974a4526a43c2f33c42832c6817bd4e1d7f3 (diff)
downloadchromium_src-a83476617999697763c3c02115d3d0f7ecfe050f.zip
chromium_src-a83476617999697763c3c02115d3d0f7ecfe050f.tar.gz
chromium_src-a83476617999697763c3c02115d3d0f7ecfe050f.tar.bz2
Add a function to scroll a platform canvas. This uses the platform scroll
functions when available, and on Linux implements a simple manual scroll function since Cairo/Skia doesn't provide one. BUG=none TEST=included unit test Review URL: http://codereview.chromium.org/2967008 git-svn-id: svn://svn.chromium.org/chrome/trunk/src@52442 0039d316-1c4b-4281-b951-d872f2087c98
Diffstat (limited to 'gfx')
-rw-r--r--gfx/blit.cc112
-rw-r--r--gfx/blit.h7
-rw-r--r--gfx/blit_unittest.cc133
-rw-r--r--gfx/gfx.gyp1
-rw-r--r--gfx/skbitmap_operations.h7
5 files changed, 252 insertions, 8 deletions
diff --git a/gfx/blit.cc b/gfx/blit.cc
index f355177..e930936 100644
--- a/gfx/blit.cc
+++ b/gfx/blit.cc
@@ -4,20 +4,50 @@
#include "gfx/blit.h"
-#if defined(OS_POSIX) && !defined(OS_MACOSX)
+#include "base/logging.h"
+#include "build/build_config.h"
+#include "gfx/point.h"
+#include "gfx/rect.h"
+#include "skia/ext/platform_canvas.h"
+#include "skia/ext/platform_device.h"
+
+#if defined(TOOLKIT_GTK)
#include <cairo/cairo.h>
#endif
#if defined(OS_MACOSX)
+#include "base/mac_util.h"
#include "base/scoped_cftyperef.h"
#endif
-#include "gfx/point.h"
-#include "gfx/rect.h"
-#include "skia/ext/platform_canvas.h"
-#include "skia/ext/platform_device.h"
namespace gfx {
+namespace {
+
+// Returns true if the given canvas has any part of itself clipped out or
+// any non-identity tranform.
+bool HasClipOrTransform(const skia::PlatformCanvas& canvas) {
+ if (!canvas.getTotalMatrix().isIdentity())
+ return true;
+
+ const SkRegion& clip_region = canvas.getTotalClip();
+ if (clip_region.isEmpty() || clip_region.isComplex())
+ return true;
+
+ // Now we know the clip is a regular rectangle, make sure it covers the
+ // entire canvas.
+ const SkBitmap& bitmap = canvas.getTopPlatformDevice().accessBitmap(false);
+ const SkIRect& clip_bounds = clip_region.getBounds();
+ if (clip_bounds.fLeft != 0 || clip_bounds.fTop != 0 ||
+ clip_bounds.fRight != bitmap.width() ||
+ clip_bounds.fBottom != bitmap.height())
+ return true;
+
+ return false;
+}
+
+} // namespace
+
void BlitContextToContext(NativeDrawingContext dst_context,
const Rect& dst_rect,
NativeDrawingContext src_context,
@@ -98,4 +128,76 @@ void BlitCanvasToCanvas(skia::PlatformCanvas *dst_canvas,
GetContextFromCanvas(src_canvas), src_origin);
}
+#if defined(OS_WIN)
+
+void ScrollCanvas(skia::PlatformCanvas* canvas,
+ const gfx::Rect& clip,
+ const gfx::Point& amount) {
+ DCHECK(!HasClipOrTransform(*canvas)); // Don't support special stuff.
+ HDC hdc = canvas->beginPlatformPaint();
+
+ RECT damaged_rect;
+ RECT r = clip.ToRECT();
+ ScrollDC(hdc, amount.x(), amount.y(), NULL, &r, NULL, &damaged_rect);
+
+ canvas->endPlatformPaint();
+}
+
+#elif defined(OS_POSIX)
+// Cairo has no nice scroll function so we do our own. On Mac it's possible to
+// use platform scroll code, but it's complex so we just use the same path
+// here. Either way it will be software-only, so it shouldn't matter much.
+
+void ScrollCanvas(skia::PlatformCanvas* canvas,
+ const gfx::Rect& in_clip,
+ const gfx::Point& amount) {
+ DCHECK(!HasClipOrTransform(*canvas)); // Don't support special stuff.
+ SkBitmap& bitmap = const_cast<SkBitmap&>(
+ canvas->getTopPlatformDevice().accessBitmap(true));
+ SkAutoLockPixels lock(bitmap);
+
+ // We expect all coords to be inside the canvas, so clip here.
+ gfx::Rect clip = in_clip.Intersect(
+ gfx::Rect(0, 0, bitmap.width(), bitmap.height()));
+
+ // Compute the set of pixels we'll actually end up painting.
+ gfx::Rect dest_rect = clip;
+ dest_rect.Offset(amount);
+ dest_rect = dest_rect.Intersect(clip);
+ if (dest_rect.size() == gfx::Size())
+ return; // Nothing to do.
+
+ // Compute the source pixels that will map to the dest_rect
+ gfx::Rect src_rect = dest_rect;
+ src_rect.Offset(-amount.x(), -amount.y());
+
+ size_t row_bytes = dest_rect.width() * 4;
+ if (amount.y() > 0) {
+ // Data is moving down, copy from the bottom up.
+ for (int y = dest_rect.height() - 1; y >= 0; y--) {
+ memcpy(bitmap.getAddr32(dest_rect.x(), dest_rect.y() + y),
+ bitmap.getAddr32(src_rect.x(), src_rect.y() + y),
+ row_bytes);
+ }
+ } else if (amount.y() < 0) {
+ // Data is moving up, copy from the top down.
+ for (int y = 0; y < dest_rect.height(); y++) {
+ memcpy(bitmap.getAddr32(dest_rect.x(), dest_rect.y() + y),
+ bitmap.getAddr32(src_rect.x(), src_rect.y() + y),
+ row_bytes);
+ }
+ } else if (amount.x() != 0) {
+ // Horizontal-only scroll. We can do it in either top-to-bottom or bottom-
+ // to-top, but have to be careful about the order for copying each row.
+ // Fortunately, memmove already handles this for us.
+ for (int y = 0; y < dest_rect.height(); y++) {
+ memmove(bitmap.getAddr32(dest_rect.x(), dest_rect.y() + y),
+ bitmap.getAddr32(src_rect.x(), src_rect.y() + y),
+ row_bytes);
+ }
+ }
+}
+
+#endif
+
} // namespace gfx
diff --git a/gfx/blit.h b/gfx/blit.h
index dd31a9e..e7ddce1 100644
--- a/gfx/blit.h
+++ b/gfx/blit.h
@@ -42,6 +42,13 @@ void BlitCanvasToCanvas(skia::PlatformCanvas *dst_canvas,
skia::PlatformCanvas *src_canvas,
const Point& src_origin);
+// Scrolls the given subset of the given canvas by the given amount.
+// The canvas should not have a clip or a transform applied, since platforms
+// may implement those operations differently.
+void ScrollCanvas(skia::PlatformCanvas* canvas,
+ const Rect& clip,
+ const Point& amount);
+
} // namespace gfx
#endif // GFX_BLIT_H_
diff --git a/gfx/blit_unittest.cc b/gfx/blit_unittest.cc
new file mode 100644
index 0000000..c5bff1e
--- /dev/null
+++ b/gfx/blit_unittest.cc
@@ -0,0 +1,133 @@
+// Copyright (c) 2010 The Chromium Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style license that can be
+// found in the LICENSE file.
+
+#include "base/basictypes.h"
+#include "gfx/blit.h"
+#include "skia/ext/platform_canvas.h"
+#include "testing/gtest/include/gtest/gtest.h"
+
+namespace {
+
+// Fills the given canvas with the values by duplicating the values into each
+// color channel for the corresponding pixel.
+//
+// Example values = {{0x0, 0x01}, {0x12, 0xFF}} would give a canvas with:
+// 0x00000000 0x01010101
+// 0x12121212 0xFFFFFFFF
+template<int w, int h>
+void SetToCanvas(skia::PlatformCanvas* canvas, uint8 values[h][w]) {
+ SkBitmap& bitmap = const_cast<SkBitmap&>(
+ canvas->getTopPlatformDevice().accessBitmap(true));
+ SkAutoLockPixels lock(bitmap);
+ ASSERT_EQ(w, bitmap.width());
+ ASSERT_EQ(h, bitmap.height());
+
+ for (int y = 0; y < h; y++) {
+ for (int x = 0; x < w; x++) {
+ uint8 value = values[y][x];
+ *bitmap.getAddr32(x, y) =
+ (value << 24) | (value << 16) | (value << 8) | value;
+ }
+ }
+}
+
+// Checks each pixel in the given canvas and see if it is made up of the given
+// values, where each value has been duplicated into each channel of the given
+// bitmap (see SetToCanvas above).
+template<int w, int h>
+void VerifyCanvasValues(skia::PlatformCanvas* canvas, uint8 values[h][w]) {
+ SkBitmap& bitmap = const_cast<SkBitmap&>(
+ canvas->getTopPlatformDevice().accessBitmap(true));
+ SkAutoLockPixels lock(bitmap);
+ ASSERT_EQ(w, bitmap.width());
+ ASSERT_EQ(h, bitmap.height());
+
+ for (int y = 0; y < h; y++) {
+ for (int x = 0; x < w; x++) {
+ uint8 value = values[y][x];
+ uint32 expected =
+ (value << 24) | (value << 16) | (value << 8) | value;
+ ASSERT_EQ(expected, *bitmap.getAddr32(x, y));
+ }
+ }
+}
+
+} // namespace
+
+TEST(Blit, ScrollCanvas) {
+ static const int kCanvasWidth = 5;
+ static const int kCanvasHeight = 5;
+ skia::PlatformCanvas canvas(kCanvasWidth, kCanvasHeight, true);
+ uint8 initial_values[kCanvasHeight][kCanvasWidth] = {
+ { 0x00, 0x01, 0x02, 0x03, 0x04 },
+ { 0x10, 0x11, 0x12, 0x13, 0x14 },
+ { 0x20, 0x21, 0x22, 0x23, 0x24 },
+ { 0x30, 0x31, 0x32, 0x33, 0x34 },
+ { 0x40, 0x41, 0x42, 0x43, 0x44 }};
+ SetToCanvas<5, 5>(&canvas, initial_values);
+
+ // Sanity check on input.
+ VerifyCanvasValues<5, 5>(&canvas, initial_values);
+
+ // Scroll none and make sure it's a NOP.
+ gfx::ScrollCanvas(&canvas,
+ gfx::Rect(0, 0, kCanvasWidth, kCanvasHeight),
+ gfx::Point(0, 0));
+ VerifyCanvasValues<5, 5>(&canvas, initial_values);
+
+ // Scroll the center 3 pixels up one.
+ gfx::Rect center_three(1, 1, 3, 3);
+ gfx::ScrollCanvas(&canvas, center_three, gfx::Point(0, -1));
+ uint8 scroll_up_expected[kCanvasHeight][kCanvasWidth] = {
+ { 0x00, 0x01, 0x02, 0x03, 0x04 },
+ { 0x10, 0x21, 0x22, 0x23, 0x14 },
+ { 0x20, 0x31, 0x32, 0x33, 0x24 },
+ { 0x30, 0x31, 0x32, 0x33, 0x34 },
+ { 0x40, 0x41, 0x42, 0x43, 0x44 }};
+ VerifyCanvasValues<5, 5>(&canvas, scroll_up_expected);
+
+ // Reset and scroll the center 3 pixels down one.
+ SetToCanvas<5, 5>(&canvas, initial_values);
+ gfx::ScrollCanvas(&canvas, center_three, gfx::Point(0, 1));
+ uint8 scroll_down_expected[kCanvasHeight][kCanvasWidth] = {
+ { 0x00, 0x01, 0x02, 0x03, 0x04 },
+ { 0x10, 0x11, 0x12, 0x13, 0x14 },
+ { 0x20, 0x11, 0x12, 0x13, 0x24 },
+ { 0x30, 0x21, 0x22, 0x23, 0x34 },
+ { 0x40, 0x41, 0x42, 0x43, 0x44 }};
+ VerifyCanvasValues<5, 5>(&canvas, scroll_down_expected);
+
+ // Reset and scroll the center 3 pixels right one.
+ SetToCanvas<5, 5>(&canvas, initial_values);
+ gfx::ScrollCanvas(&canvas, center_three, gfx::Point(1, 0));
+ uint8 scroll_right_expected[kCanvasHeight][kCanvasWidth] = {
+ { 0x00, 0x01, 0x02, 0x03, 0x04 },
+ { 0x10, 0x11, 0x11, 0x12, 0x14 },
+ { 0x20, 0x21, 0x21, 0x22, 0x24 },
+ { 0x30, 0x31, 0x31, 0x32, 0x34 },
+ { 0x40, 0x41, 0x42, 0x43, 0x44 }};
+ VerifyCanvasValues<5, 5>(&canvas, scroll_right_expected);
+
+ // Reset and scroll the center 3 pixels left one.
+ SetToCanvas<5, 5>(&canvas, initial_values);
+ gfx::ScrollCanvas(&canvas, center_three, gfx::Point(-1, 0));
+ uint8 scroll_left_expected[kCanvasHeight][kCanvasWidth] = {
+ { 0x00, 0x01, 0x02, 0x03, 0x04 },
+ { 0x10, 0x12, 0x13, 0x13, 0x14 },
+ { 0x20, 0x22, 0x23, 0x23, 0x24 },
+ { 0x30, 0x32, 0x33, 0x33, 0x34 },
+ { 0x40, 0x41, 0x42, 0x43, 0x44 }};
+ VerifyCanvasValues<5, 5>(&canvas, scroll_left_expected);
+
+ // Diagonal scroll.
+ SetToCanvas<5, 5>(&canvas, initial_values);
+ gfx::ScrollCanvas(&canvas, center_three, gfx::Point(2, 2));
+ uint8 scroll_diagonal_expected[kCanvasHeight][kCanvasWidth] = {
+ { 0x00, 0x01, 0x02, 0x03, 0x04 },
+ { 0x10, 0x11, 0x12, 0x13, 0x14 },
+ { 0x20, 0x21, 0x22, 0x23, 0x24 },
+ { 0x30, 0x31, 0x32, 0x11, 0x34 },
+ { 0x40, 0x41, 0x42, 0x43, 0x44 }};
+ VerifyCanvasValues<5, 5>(&canvas, scroll_diagonal_expected);
+}
diff --git a/gfx/gfx.gyp b/gfx/gfx.gyp
index 859c6f3f..d51919b 100644
--- a/gfx/gfx.gyp
+++ b/gfx/gfx.gyp
@@ -17,6 +17,7 @@
'../testing/gtest.gyp:gtest',
],
'sources': [
+ 'blit_unittest.cc',
'codec/jpeg_codec_unittest.cc',
'codec/png_codec_unittest.cc',
'color_utils_unittest.cc',
diff --git a/gfx/skbitmap_operations.h b/gfx/skbitmap_operations.h
index ec55e55..1308308 100644
--- a/gfx/skbitmap_operations.h
+++ b/gfx/skbitmap_operations.h
@@ -2,8 +2,8 @@
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
-#ifndef APP_GFX_SKBITMAP_OPERATIONS_H_
-#define APP_GFX_SKBITMAP_OPERATIONS_H_
+#ifndef GFX_SKBITMAP_OPERATIONS_H_
+#define GFX_SKBITMAP_OPERATIONS_H_
#include "gfx/color_utils.h"
#include "testing/gtest/include/gtest/gtest_prod.h"
@@ -81,6 +81,7 @@ class SkBitmapOperations {
// Makes a bitmap half has large in each direction by averaging groups of
// 4 pixels. This is one step in generating a mipmap.
static SkBitmap DownsampleByTwo(const SkBitmap& bitmap);
+
private:
SkBitmapOperations(); // Class for scoping only.
@@ -88,4 +89,4 @@ class SkBitmapOperations {
FRIEND_TEST(SkBitmapOperationsTest, DownsampleByTwoSmall);
};
-#endif // APP_GFX_SKBITMAP_OPERATIONS_H_
+#endif // GFX_SKBITMAP_OPERATIONS_H_