blob: 59e565bba049f18f7fab4f08303140b821a0b104 [file] [log] [blame]
junov@chromium.org995beb62013-03-28 13:49:22 +00001
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
16class GrContextFactory;
17class GrContext;
18#endif
19
20enum SurfaceType {
21 kRaster_SurfaceType,
22 kGpu_SurfaceType,
23 kPicture_SurfaceType
24};
25
26static SkSurface* createSurface(SurfaceType surfaceType, GrContext* context) {
27 static const SkImage::Info imageSpec = {
28 10, // width
29 10, // height
30 SkImage::kPMColor_ColorType,
31 SkImage::kPremul_AlphaType
32 };
33
34 switch (surfaceType) {
35 case kRaster_SurfaceType:
36 return SkSurface::NewRaster(imageSpec);
37 case kGpu_SurfaceType:
junov@chromium.org995beb62013-03-28 13:49:22 +000038 SkASSERT(NULL != context);
39 return SkSurface::NewRenderTarget(context, imageSpec);
junov@chromium.org995beb62013-03-28 13:49:22 +000040 case kPicture_SurfaceType:
41 return SkSurface::NewPicture(10, 10);
42 }
43 SkASSERT(0);
44 return NULL;
45}
46
47static void TestSurfaceCopyOnWrite(skiatest::Reporter* reporter, SurfaceType surfaceType,
48 GrContext* context) {
49 // Verify that the right canvas commands trigger a copy on write
50 SkSurface* surface = createSurface(surfaceType, context);
51 SkAutoTUnref<SkSurface> aur_surface(surface);
52 SkCanvas* canvas = surface->getCanvas();
53
54 const SkRect testRect =
55 SkRect::MakeXYWH(SkIntToScalar(0), SkIntToScalar(0),
56 SkIntToScalar(4), SkIntToScalar(5));
57 SkMatrix testMatrix;
58 testMatrix.reset();
59 testMatrix.setScale(SkIntToScalar(2), SkIntToScalar(3));
60
61 SkPath testPath;
62 testPath.addRect(SkRect::MakeXYWH(SkIntToScalar(0), SkIntToScalar(0),
63 SkIntToScalar(2), SkIntToScalar(1)));
64
65 const SkIRect testIRect = SkIRect::MakeXYWH(0, 0, 2, 1);
66
67 SkRegion testRegion;
68 testRegion.setRect(testIRect);
69
70
71 const SkColor testColor = 0x01020304;
72 const SkPaint testPaint;
73 const SkPoint testPoints[3] = {
74 {SkIntToScalar(0), SkIntToScalar(0)},
75 {SkIntToScalar(2), SkIntToScalar(1)},
76 {SkIntToScalar(0), SkIntToScalar(2)}
77 };
78 const size_t testPointCount = 3;
79
80 SkBitmap testBitmap;
81 testBitmap.setConfig(SkBitmap::kARGB_8888_Config, 10, 10);
82 testBitmap.allocPixels();
83
84 SkRRect testRRect;
85 testRRect.setRectXY(testRect, SK_Scalar1, SK_Scalar1);
86
87 SkString testText("Hello World");
88 const SkPoint testPoints2[] = {
89 { SkIntToScalar(0), SkIntToScalar(1) },
90 { SkIntToScalar(1), SkIntToScalar(1) },
91 { SkIntToScalar(2), SkIntToScalar(1) },
92 { SkIntToScalar(3), SkIntToScalar(1) },
93 { SkIntToScalar(4), SkIntToScalar(1) },
94 { SkIntToScalar(5), SkIntToScalar(1) },
95 { SkIntToScalar(6), SkIntToScalar(1) },
96 { SkIntToScalar(7), SkIntToScalar(1) },
97 { SkIntToScalar(8), SkIntToScalar(1) },
98 { SkIntToScalar(9), SkIntToScalar(1) },
99 { SkIntToScalar(10), SkIntToScalar(1) },
100 };
101
102#define EXPECT_COPY_ON_WRITE(command) \
103 { \
104 SkImage* imageBefore = surface->newImageShapshot(); \
105 SkAutoTUnref<SkImage> aur_before(imageBefore); \
106 canvas-> command ; \
107 SkImage* imageAfter = surface->newImageShapshot(); \
108 SkAutoTUnref<SkImage> aur_after(imageAfter); \
109 REPORTER_ASSERT(reporter, imageBefore != imageAfter); \
110 }
111
112 EXPECT_COPY_ON_WRITE(clear(testColor))
113 EXPECT_COPY_ON_WRITE(drawPaint(testPaint))
114 EXPECT_COPY_ON_WRITE(drawPoints(SkCanvas::kPoints_PointMode, testPointCount, testPoints, \
115 testPaint))
116 EXPECT_COPY_ON_WRITE(drawOval(testRect, testPaint))
117 EXPECT_COPY_ON_WRITE(drawRect(testRect, testPaint))
118 EXPECT_COPY_ON_WRITE(drawRRect(testRRect, testPaint))
119 EXPECT_COPY_ON_WRITE(drawPath(testPath, testPaint))
120 EXPECT_COPY_ON_WRITE(drawBitmap(testBitmap, 0, 0))
121 EXPECT_COPY_ON_WRITE(drawBitmapRect(testBitmap, NULL, testRect))
122 EXPECT_COPY_ON_WRITE(drawBitmapMatrix(testBitmap, testMatrix, NULL))
123 EXPECT_COPY_ON_WRITE(drawBitmapNine(testBitmap, testIRect, testRect, NULL))
124 EXPECT_COPY_ON_WRITE(drawSprite(testBitmap, 0, 0, NULL))
125 EXPECT_COPY_ON_WRITE(drawText(testText.c_str(), testText.size(), 0, 1, testPaint))
126 EXPECT_COPY_ON_WRITE(drawPosText(testText.c_str(), testText.size(), testPoints2, \
127 testPaint))
128 EXPECT_COPY_ON_WRITE(drawTextOnPath(testText.c_str(), testText.size(), testPath, NULL, \
129 testPaint))
130}
131
junov@chromium.orgaf058352013-04-03 15:03:26 +0000132static void TestSurfaceWritableAfterSnapshotRelease(skiatest::Reporter* reporter,
133 SurfaceType surfaceType,
134 GrContext* context) {
135 // This test succeeds by not triggering an assertion.
136 // The test verifies that the surface remains writable (usable) after
137 // acquiring and releasing a snapshot without triggering a copy on write.
138 SkSurface* surface = createSurface(surfaceType, context);
139 SkAutoTUnref<SkSurface> aur_surface(surface);
140 SkCanvas* canvas = surface->getCanvas();
141 canvas->clear(1);
142 surface->newImageShapshot()->unref(); // Create and destroy SkImage
143 canvas->clear(2);
junov@chromium.org995beb62013-03-28 13:49:22 +0000144}
145
junov@chromium.orgaf058352013-04-03 15:03:26 +0000146static void TestSurface(skiatest::Reporter* reporter, GrContextFactory* factory) {
147 TestSurfaceCopyOnWrite(reporter, kRaster_SurfaceType, NULL);
148 TestSurfaceCopyOnWrite(reporter, kPicture_SurfaceType, NULL);
149 TestSurfaceWritableAfterSnapshotRelease(reporter, kRaster_SurfaceType, NULL);
150 TestSurfaceWritableAfterSnapshotRelease(reporter, kPicture_SurfaceType, NULL);
junov@chromium.org995beb62013-03-28 13:49:22 +0000151#if SK_SUPPORT_GPU
junov@chromium.orgaf058352013-04-03 15:03:26 +0000152 if (NULL != factory) {
153 GrContext* context = factory->get(GrContextFactory::kNative_GLContextType);
154 TestSurfaceCopyOnWrite(reporter, kGpu_SurfaceType, context);
155 TestSurfaceWritableAfterSnapshotRelease(reporter, kGpu_SurfaceType, context);
156 }
junov@chromium.org995beb62013-03-28 13:49:22 +0000157#endif
158}
159
160#include "TestClassDef.h"
junov@chromium.orgaf058352013-04-03 15:03:26 +0000161DEFINE_GPUTESTCLASS("Surface", SurfaceTestClass, TestSurface)