Index: tests/SurfaceTest.cpp |
=================================================================== |
--- tests/SurfaceTest.cpp (revision 0) |
+++ tests/SurfaceTest.cpp (revision 0) |
@@ -0,0 +1,152 @@ |
+ |
+/* |
+ * Copyright 2013 Google Inc. |
+ * |
+ * Use of this source code is governed by a BSD-style license that can be |
+ * found in the LICENSE file. |
+ */ |
+#include "SkCanvas.h" |
+#include "SkRRect.h" |
+#include "SkSurface.h" |
+#include "Test.h" |
+ |
+#if SK_SUPPORT_GPU |
+#include "GrContextFactory.h" |
+#else |
+class GrContextFactory; |
+class GrContext; |
+#endif |
+ |
+static SkSurface* createSurface(GrContext* context) { |
+ SkImage::Info imageSpec = { |
+ 10, // width |
+ 10, // height |
+ SkImage::kPMColor_ColorType, |
+ SkImage::kPremul_AlphaType |
+ }; |
+ SkSurface* surface; |
+ bool useGpu = NULL != context; |
+#if SK_SUPPORT_GPU |
+ if (useGpu) { |
+ surface = SkSurface::NewRenderTarget(context, imageSpec); |
+ } else { |
+ surface = SkSurface::NewRaster(imageSpec); |
+ } |
+#else |
+ SkASSERT(!useGpu); |
+ surface = SkSurface::NewRaster(imageSpec); |
+#endif |
+ return surface; |
+} |
+ |
+static void TestSurfaceCopyOnWrite(skiatest::Reporter* reporter, GrContext* context) { |
+ // Verify that the right canvas commands trigger a copy on write |
reed1
2013/03/27 19:46:59
Can this be refactored to take different factories
|
+ SkSurface* surface = createSurface(context); |
+ SkAutoTUnref<SkSurface> aur_surface(surface); |
+ SkCanvas* canvas = surface->getCanvas(); |
+ |
+ const SkRect testRect = |
+ SkRect::MakeXYWH(SkIntToScalar(0), SkIntToScalar(0), |
+ SkIntToScalar(4), SkIntToScalar(5)); |
+ SkMatrix testMatrix; |
+ testMatrix.reset(); |
+ testMatrix.setScale(SkIntToScalar(2), SkIntToScalar(3)); |
+ |
+ SkPath testPath; |
+ testPath.addRect(SkRect::MakeXYWH(SkIntToScalar(0), SkIntToScalar(0), |
+ SkIntToScalar(2), SkIntToScalar(1))); |
+ |
+ const SkIRect testIRect = SkIRect::MakeXYWH(0, 0, 2, 1); |
+ |
+ SkRegion testRegion; |
+ testRegion.setRect(testIRect); |
+ |
+ |
+ const SkColor testColor = 0x01020304; |
+ const SkPaint testPaint; |
+ const SkPoint testPoints[3] = { |
+ {SkIntToScalar(0), SkIntToScalar(0)}, |
+ {SkIntToScalar(2), SkIntToScalar(1)}, |
+ {SkIntToScalar(0), SkIntToScalar(2)} |
+ }; |
+ const size_t testPointCount = 3; |
+ |
+ SkBitmap testBitmap; |
+ testBitmap.setConfig(SkBitmap::kARGB_8888_Config, 10, 10); |
+ testBitmap.allocPixels(); |
+ |
+ SkRRect testRRect; |
+ testRRect.setRectXY(testRect, SK_Scalar1, SK_Scalar1); |
+ |
+ SkString testText("Hello World"); |
+ SkPoint testPoints2[] = { |
reed1
2013/03/27 19:46:59
is this array used?
reed1
2013/03/27 20:35:54
doh, my "Find" somehow missed that. sorry.
reed1
2013/03/27 20:36:19
const?
|
+ { SkIntToScalar(0), SkIntToScalar(1) }, |
+ { SkIntToScalar(1), SkIntToScalar(1) }, |
+ { SkIntToScalar(2), SkIntToScalar(1) }, |
+ { SkIntToScalar(3), SkIntToScalar(1) }, |
+ { SkIntToScalar(4), SkIntToScalar(1) }, |
+ { SkIntToScalar(5), SkIntToScalar(1) }, |
+ { SkIntToScalar(6), SkIntToScalar(1) }, |
+ { SkIntToScalar(7), SkIntToScalar(1) }, |
+ { SkIntToScalar(8), SkIntToScalar(1) }, |
+ { SkIntToScalar(9), SkIntToScalar(1) }, |
+ { SkIntToScalar(10), SkIntToScalar(1) }, |
+ }; |
+ |
+#define EXPECT_COPY_ON_WRITE(command, expectation) \ |
+ { \ |
+ SkImage* imageBefore = surface->newImageShapshot(); \ |
+ SkAutoTUnref<SkImage> aur_before(imageBefore); \ |
+ canvas-> command ; \ |
+ SkImage* imageAfter = surface->newImageShapshot(); \ |
+ SkAutoTUnref<SkImage> aur_after(imageAfter); \ |
+ bool didCopyOnWrite = imageBefore != imageAfter; \ |
+ REPORTER_ASSERT(reporter, didCopyOnWrite == expectation); \ |
+ } |
+ |
+ EXPECT_COPY_ON_WRITE(translate(SkIntToScalar(1), SkIntToScalar(2)), false) |
+ EXPECT_COPY_ON_WRITE(scale(SkIntToScalar(1), SkIntToScalar(2)), false) |
+ EXPECT_COPY_ON_WRITE(rotate(SkIntToScalar(1)), false) |
+ EXPECT_COPY_ON_WRITE(skew(SkIntToScalar(1), SkIntToScalar(2)), false) |
+ EXPECT_COPY_ON_WRITE(concat(testMatrix), false) |
+ EXPECT_COPY_ON_WRITE(setMatrix(testMatrix), false) |
+ EXPECT_COPY_ON_WRITE(clipRect(testRect), false) |
+ EXPECT_COPY_ON_WRITE(clipPath(testPath), false) |
+ EXPECT_COPY_ON_WRITE(clipRegion(testRegion, SkRegion::kReplace_Op), false) |
+ EXPECT_COPY_ON_WRITE(drawData(testText.c_str(), testText.size()), false) |
+ |
+ |
+ EXPECT_COPY_ON_WRITE(clear(testColor), true) |
+ EXPECT_COPY_ON_WRITE(drawPaint(testPaint), true) |
+ EXPECT_COPY_ON_WRITE(drawPoints(SkCanvas::kPoints_PointMode, testPointCount, testPoints, \ |
+ testPaint), true) |
+ EXPECT_COPY_ON_WRITE(drawOval(testRect, testPaint), true) |
+ EXPECT_COPY_ON_WRITE(drawRect(testRect, testPaint), true) |
+ EXPECT_COPY_ON_WRITE(drawRRect(testRRect, testPaint), true) |
+ EXPECT_COPY_ON_WRITE(drawPath(testPath, testPaint), true) |
+ EXPECT_COPY_ON_WRITE(drawBitmap(testBitmap, 0, 0), true) |
+ EXPECT_COPY_ON_WRITE(drawBitmapRect(testBitmap, NULL, testRect), true) |
+ EXPECT_COPY_ON_WRITE(drawBitmapMatrix(testBitmap, testMatrix, NULL), true) |
+ EXPECT_COPY_ON_WRITE(drawBitmapNine(testBitmap, testIRect, testRect, NULL), true) |
+ EXPECT_COPY_ON_WRITE(drawSprite(testBitmap, 0, 0, NULL), true) |
+ EXPECT_COPY_ON_WRITE(drawText(testText.c_str(), testText.size(), 0, 1, testPaint), true) |
+ EXPECT_COPY_ON_WRITE(drawPosText(testText.c_str(), testText.size(), testPoints2, \ |
+ testPaint), true) |
+ EXPECT_COPY_ON_WRITE(drawTextOnPath(testText.c_str(), testText.size(), testPath, NULL, \ |
+ testPaint), true) |
+} |
+ |
+static void TestSurface(skiatest::Reporter* reporter) { |
+ TestSurfaceCopyOnWrite(reporter, NULL); |
+} |
+ |
+static void TestSurfaceGpu(skiatest::Reporter* reporter, GrContextFactory* factory) { |
+#if SK_SUPPORT_GPU |
+ GrContext* context = factory->get(GrContextFactory::kNative_GLContextType); |
+ TestSurfaceCopyOnWrite(reporter, context); |
+#endif |
+} |
+ |
+#include "TestClassDef.h" |
+DEFINE_TESTCLASS("Surface", SurfaceTestClass, TestSurface) |
+DEFINE_GPUTESTCLASS("SurfaceGpu", SurfaceGpuTestClass, TestSurfaceGpu) |