OLD | NEW |
---|---|
(Empty) | |
1 | |
2 /* | |
3 * Copyright 2013 Google Inc. | |
4 * | |
5 * Use of this source code is governed by a BSD-style license that can be | |
6 * found in the LICENSE file. | |
7 */ | |
8 #include "SkCanvas.h" | |
9 #include "SkRRect.h" | |
10 #include "SkSurface.h" | |
11 #include "Test.h" | |
12 | |
13 #if SK_SUPPORT_GPU | |
14 #include "GrContextFactory.h" | |
15 #else | |
16 class GrContextFactory; | |
17 class GrContext; | |
18 #endif | |
19 | |
20 static SkSurface* createSurface(GrContext* context) { | |
21 SkImage::Info imageSpec = { | |
22 10, // width | |
23 10, // height | |
24 SkImage::kPMColor_ColorType, | |
25 SkImage::kPremul_AlphaType | |
26 }; | |
27 SkSurface* surface; | |
28 bool useGpu = NULL != context; | |
29 #if SK_SUPPORT_GPU | |
30 if (useGpu) { | |
31 surface = SkSurface::NewRenderTarget(context, imageSpec); | |
32 } else { | |
33 surface = SkSurface::NewRaster(imageSpec); | |
34 } | |
35 #else | |
36 SkASSERT(!useGpu); | |
37 surface = SkSurface::NewRaster(imageSpec); | |
38 #endif | |
39 return surface; | |
40 } | |
41 | |
42 static void TestSurfaceCopyOnWrite(skiatest::Reporter* reporter, GrContext* cont ext) { | |
43 // 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
| |
44 SkSurface* surface = createSurface(context); | |
45 SkAutoTUnref<SkSurface> aur_surface(surface); | |
46 SkCanvas* canvas = surface->getCanvas(); | |
47 | |
48 const SkRect testRect = | |
49 SkRect::MakeXYWH(SkIntToScalar(0), SkIntToScalar(0), | |
50 SkIntToScalar(4), SkIntToScalar(5)); | |
51 SkMatrix testMatrix; | |
52 testMatrix.reset(); | |
53 testMatrix.setScale(SkIntToScalar(2), SkIntToScalar(3)); | |
54 | |
55 SkPath testPath; | |
56 testPath.addRect(SkRect::MakeXYWH(SkIntToScalar(0), SkIntToScalar(0), | |
57 SkIntToScalar(2), SkIntToScalar(1))); | |
58 | |
59 const SkIRect testIRect = SkIRect::MakeXYWH(0, 0, 2, 1); | |
60 | |
61 SkRegion testRegion; | |
62 testRegion.setRect(testIRect); | |
63 | |
64 | |
65 const SkColor testColor = 0x01020304; | |
66 const SkPaint testPaint; | |
67 const SkPoint testPoints[3] = { | |
68 {SkIntToScalar(0), SkIntToScalar(0)}, | |
69 {SkIntToScalar(2), SkIntToScalar(1)}, | |
70 {SkIntToScalar(0), SkIntToScalar(2)} | |
71 }; | |
72 const size_t testPointCount = 3; | |
73 | |
74 SkBitmap testBitmap; | |
75 testBitmap.setConfig(SkBitmap::kARGB_8888_Config, 10, 10); | |
76 testBitmap.allocPixels(); | |
77 | |
78 SkRRect testRRect; | |
79 testRRect.setRectXY(testRect, SK_Scalar1, SK_Scalar1); | |
80 | |
81 SkString testText("Hello World"); | |
82 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?
| |
83 { SkIntToScalar(0), SkIntToScalar(1) }, | |
84 { SkIntToScalar(1), SkIntToScalar(1) }, | |
85 { SkIntToScalar(2), SkIntToScalar(1) }, | |
86 { SkIntToScalar(3), SkIntToScalar(1) }, | |
87 { SkIntToScalar(4), SkIntToScalar(1) }, | |
88 { SkIntToScalar(5), SkIntToScalar(1) }, | |
89 { SkIntToScalar(6), SkIntToScalar(1) }, | |
90 { SkIntToScalar(7), SkIntToScalar(1) }, | |
91 { SkIntToScalar(8), SkIntToScalar(1) }, | |
92 { SkIntToScalar(9), SkIntToScalar(1) }, | |
93 { SkIntToScalar(10), SkIntToScalar(1) }, | |
94 }; | |
95 | |
96 #define EXPECT_COPY_ON_WRITE(command, expectation) \ | |
97 { \ | |
98 SkImage* imageBefore = surface->newImageShapshot(); \ | |
99 SkAutoTUnref<SkImage> aur_before(imageBefore); \ | |
100 canvas-> command ; \ | |
101 SkImage* imageAfter = surface->newImageShapshot(); \ | |
102 SkAutoTUnref<SkImage> aur_after(imageAfter); \ | |
103 bool didCopyOnWrite = imageBefore != imageAfter; \ | |
104 REPORTER_ASSERT(reporter, didCopyOnWrite == expectation); \ | |
105 } | |
106 | |
107 EXPECT_COPY_ON_WRITE(translate(SkIntToScalar(1), SkIntToScalar(2)), false) | |
108 EXPECT_COPY_ON_WRITE(scale(SkIntToScalar(1), SkIntToScalar(2)), false) | |
109 EXPECT_COPY_ON_WRITE(rotate(SkIntToScalar(1)), false) | |
110 EXPECT_COPY_ON_WRITE(skew(SkIntToScalar(1), SkIntToScalar(2)), false) | |
111 EXPECT_COPY_ON_WRITE(concat(testMatrix), false) | |
112 EXPECT_COPY_ON_WRITE(setMatrix(testMatrix), false) | |
113 EXPECT_COPY_ON_WRITE(clipRect(testRect), false) | |
114 EXPECT_COPY_ON_WRITE(clipPath(testPath), false) | |
115 EXPECT_COPY_ON_WRITE(clipRegion(testRegion, SkRegion::kReplace_Op), false) | |
116 EXPECT_COPY_ON_WRITE(drawData(testText.c_str(), testText.size()), false) | |
117 | |
118 | |
119 EXPECT_COPY_ON_WRITE(clear(testColor), true) | |
120 EXPECT_COPY_ON_WRITE(drawPaint(testPaint), true) | |
121 EXPECT_COPY_ON_WRITE(drawPoints(SkCanvas::kPoints_PointMode, testPointCount, testPoints, \ | |
122 testPaint), true) | |
123 EXPECT_COPY_ON_WRITE(drawOval(testRect, testPaint), true) | |
124 EXPECT_COPY_ON_WRITE(drawRect(testRect, testPaint), true) | |
125 EXPECT_COPY_ON_WRITE(drawRRect(testRRect, testPaint), true) | |
126 EXPECT_COPY_ON_WRITE(drawPath(testPath, testPaint), true) | |
127 EXPECT_COPY_ON_WRITE(drawBitmap(testBitmap, 0, 0), true) | |
128 EXPECT_COPY_ON_WRITE(drawBitmapRect(testBitmap, NULL, testRect), true) | |
129 EXPECT_COPY_ON_WRITE(drawBitmapMatrix(testBitmap, testMatrix, NULL), true) | |
130 EXPECT_COPY_ON_WRITE(drawBitmapNine(testBitmap, testIRect, testRect, NULL), true) | |
131 EXPECT_COPY_ON_WRITE(drawSprite(testBitmap, 0, 0, NULL), true) | |
132 EXPECT_COPY_ON_WRITE(drawText(testText.c_str(), testText.size(), 0, 1, testP aint), true) | |
133 EXPECT_COPY_ON_WRITE(drawPosText(testText.c_str(), testText.size(), testPoin ts2, \ | |
134 testPaint), true) | |
135 EXPECT_COPY_ON_WRITE(drawTextOnPath(testText.c_str(), testText.size(), testP ath, NULL, \ | |
136 testPaint), true) | |
137 } | |
138 | |
139 static void TestSurface(skiatest::Reporter* reporter) { | |
140 TestSurfaceCopyOnWrite(reporter, NULL); | |
141 } | |
142 | |
143 static void TestSurfaceGpu(skiatest::Reporter* reporter, GrContextFactory* facto ry) { | |
144 #if SK_SUPPORT_GPU | |
145 GrContext* context = factory->get(GrContextFactory::kNative_GLContextType); | |
146 TestSurfaceCopyOnWrite(reporter, context); | |
147 #endif | |
148 } | |
149 | |
150 #include "TestClassDef.h" | |
151 DEFINE_TESTCLASS("Surface", SurfaceTestClass, TestSurface) | |
152 DEFINE_GPUTESTCLASS("SurfaceGpu", SurfaceGpuTestClass, TestSurfaceGpu) | |
OLD | NEW |