blob: b580999fd59c3e94cfd62f409bfb761eaadd0ba8 [file] [log] [blame]
junov@chromium.org995beb62013-03-28 13:49:22 +00001/*
2 * Copyright 2013 Google Inc.
3 *
4 * Use of this source code is governed by a BSD-style license that can be
5 * found in the LICENSE file.
6 */
tfarina@chromium.org4ee16bf2014-01-10 22:08:27 +00007
junov@chromium.org995beb62013-03-28 13:49:22 +00008#include "SkCanvas.h"
9#include "SkRRect.h"
10#include "SkSurface.h"
11#include "Test.h"
tfarina@chromium.org4ee16bf2014-01-10 22:08:27 +000012#include "TestClassDef.h"
junov@chromium.org995beb62013-03-28 13:49:22 +000013
14#if SK_SUPPORT_GPU
15#include "GrContextFactory.h"
16#else
17class GrContextFactory;
18class GrContext;
19#endif
20
21enum SurfaceType {
22 kRaster_SurfaceType,
23 kGpu_SurfaceType,
24 kPicture_SurfaceType
25};
26
27static SkSurface* createSurface(SurfaceType surfaceType, GrContext* context) {
reed@google.com2bd8b812013-11-01 13:46:54 +000028 static const SkImageInfo imageSpec = {
junov@chromium.org995beb62013-03-28 13:49:22 +000029 10, // width
30 10, // height
reed@google.com2bd8b812013-11-01 13:46:54 +000031 kPMColor_SkColorType,
reed@google.comd28ba802013-09-20 19:33:52 +000032 kPremul_SkAlphaType
junov@chromium.org995beb62013-03-28 13:49:22 +000033 };
34
35 switch (surfaceType) {
36 case kRaster_SurfaceType:
37 return SkSurface::NewRaster(imageSpec);
38 case kGpu_SurfaceType:
junov@chromium.org8bc9edc2013-04-03 15:25:46 +000039#if SK_SUPPORT_GPU
junov@chromium.org995beb62013-03-28 13:49:22 +000040 SkASSERT(NULL != context);
41 return SkSurface::NewRenderTarget(context, imageSpec);
junov@chromium.org8bc9edc2013-04-03 15:25:46 +000042#else
43 SkASSERT(0);
44#endif
junov@chromium.org995beb62013-03-28 13:49:22 +000045 case kPicture_SurfaceType:
46 return SkSurface::NewPicture(10, 10);
47 }
48 SkASSERT(0);
49 return NULL;
50}
51
52static void TestSurfaceCopyOnWrite(skiatest::Reporter* reporter, SurfaceType surfaceType,
53 GrContext* context) {
54 // Verify that the right canvas commands trigger a copy on write
55 SkSurface* surface = createSurface(surfaceType, context);
56 SkAutoTUnref<SkSurface> aur_surface(surface);
57 SkCanvas* canvas = surface->getCanvas();
58
59 const SkRect testRect =
60 SkRect::MakeXYWH(SkIntToScalar(0), SkIntToScalar(0),
61 SkIntToScalar(4), SkIntToScalar(5));
62 SkMatrix testMatrix;
63 testMatrix.reset();
64 testMatrix.setScale(SkIntToScalar(2), SkIntToScalar(3));
65
66 SkPath testPath;
67 testPath.addRect(SkRect::MakeXYWH(SkIntToScalar(0), SkIntToScalar(0),
68 SkIntToScalar(2), SkIntToScalar(1)));
69
70 const SkIRect testIRect = SkIRect::MakeXYWH(0, 0, 2, 1);
71
72 SkRegion testRegion;
73 testRegion.setRect(testIRect);
74
75
76 const SkColor testColor = 0x01020304;
77 const SkPaint testPaint;
78 const SkPoint testPoints[3] = {
79 {SkIntToScalar(0), SkIntToScalar(0)},
80 {SkIntToScalar(2), SkIntToScalar(1)},
81 {SkIntToScalar(0), SkIntToScalar(2)}
82 };
83 const size_t testPointCount = 3;
84
85 SkBitmap testBitmap;
86 testBitmap.setConfig(SkBitmap::kARGB_8888_Config, 10, 10);
87 testBitmap.allocPixels();
robertphillips@google.comd1ce77d2013-10-09 12:51:09 +000088 testBitmap.eraseColor(0);
junov@chromium.org995beb62013-03-28 13:49:22 +000089
90 SkRRect testRRect;
91 testRRect.setRectXY(testRect, SK_Scalar1, SK_Scalar1);
92
93 SkString testText("Hello World");
94 const SkPoint testPoints2[] = {
95 { SkIntToScalar(0), SkIntToScalar(1) },
96 { SkIntToScalar(1), SkIntToScalar(1) },
97 { SkIntToScalar(2), SkIntToScalar(1) },
98 { SkIntToScalar(3), SkIntToScalar(1) },
99 { SkIntToScalar(4), SkIntToScalar(1) },
100 { SkIntToScalar(5), SkIntToScalar(1) },
101 { SkIntToScalar(6), SkIntToScalar(1) },
102 { SkIntToScalar(7), SkIntToScalar(1) },
103 { SkIntToScalar(8), SkIntToScalar(1) },
104 { SkIntToScalar(9), SkIntToScalar(1) },
105 { SkIntToScalar(10), SkIntToScalar(1) },
106 };
107
108#define EXPECT_COPY_ON_WRITE(command) \
109 { \
junov@chromium.org5ee449a2013-04-12 20:20:50 +0000110 SkImage* imageBefore = surface->newImageSnapshot(); \
junov@chromium.org995beb62013-03-28 13:49:22 +0000111 SkAutoTUnref<SkImage> aur_before(imageBefore); \
112 canvas-> command ; \
junov@chromium.org5ee449a2013-04-12 20:20:50 +0000113 SkImage* imageAfter = surface->newImageSnapshot(); \
junov@chromium.org995beb62013-03-28 13:49:22 +0000114 SkAutoTUnref<SkImage> aur_after(imageAfter); \
115 REPORTER_ASSERT(reporter, imageBefore != imageAfter); \
116 }
117
118 EXPECT_COPY_ON_WRITE(clear(testColor))
119 EXPECT_COPY_ON_WRITE(drawPaint(testPaint))
120 EXPECT_COPY_ON_WRITE(drawPoints(SkCanvas::kPoints_PointMode, testPointCount, testPoints, \
121 testPaint))
122 EXPECT_COPY_ON_WRITE(drawOval(testRect, testPaint))
123 EXPECT_COPY_ON_WRITE(drawRect(testRect, testPaint))
124 EXPECT_COPY_ON_WRITE(drawRRect(testRRect, testPaint))
125 EXPECT_COPY_ON_WRITE(drawPath(testPath, testPaint))
126 EXPECT_COPY_ON_WRITE(drawBitmap(testBitmap, 0, 0))
127 EXPECT_COPY_ON_WRITE(drawBitmapRect(testBitmap, NULL, testRect))
128 EXPECT_COPY_ON_WRITE(drawBitmapMatrix(testBitmap, testMatrix, NULL))
129 EXPECT_COPY_ON_WRITE(drawBitmapNine(testBitmap, testIRect, testRect, NULL))
130 EXPECT_COPY_ON_WRITE(drawSprite(testBitmap, 0, 0, NULL))
131 EXPECT_COPY_ON_WRITE(drawText(testText.c_str(), testText.size(), 0, 1, testPaint))
132 EXPECT_COPY_ON_WRITE(drawPosText(testText.c_str(), testText.size(), testPoints2, \
133 testPaint))
134 EXPECT_COPY_ON_WRITE(drawTextOnPath(testText.c_str(), testText.size(), testPath, NULL, \
135 testPaint))
136}
137
junov@chromium.orgaf058352013-04-03 15:03:26 +0000138static void TestSurfaceWritableAfterSnapshotRelease(skiatest::Reporter* reporter,
139 SurfaceType surfaceType,
140 GrContext* context) {
141 // This test succeeds by not triggering an assertion.
142 // The test verifies that the surface remains writable (usable) after
143 // acquiring and releasing a snapshot without triggering a copy on write.
commit-bot@chromium.org4d24b742013-07-25 23:29:40 +0000144 SkAutoTUnref<SkSurface> surface(createSurface(surfaceType, context));
junov@chromium.orgaf058352013-04-03 15:03:26 +0000145 SkCanvas* canvas = surface->getCanvas();
146 canvas->clear(1);
junov@chromium.org5ee449a2013-04-12 20:20:50 +0000147 surface->newImageSnapshot()->unref(); // Create and destroy SkImage
commit-bot@chromium.org4d24b742013-07-25 23:29:40 +0000148 canvas->clear(2); // Must not assert internally
junov@chromium.org995beb62013-03-28 13:49:22 +0000149}
junov@chromium.orgda904742013-05-01 22:38:16 +0000150
junov@chromium.orgb516a412013-05-01 22:49:59 +0000151#if SK_SUPPORT_GPU
commit-bot@chromium.org4d24b742013-07-25 23:29:40 +0000152static void Test_crbug263329(skiatest::Reporter* reporter,
153 GrContext* context) {
154 // This is a regression test for crbug.com/263329
155 // Bug was caused by onCopyOnWrite releasing the old surface texture
156 // back to the scratch texture pool even though the texture is used
157 // by and active SkImage_Gpu.
158 SkAutoTUnref<SkSurface> surface1(createSurface(kGpu_SurfaceType, context));
159 SkAutoTUnref<SkSurface> surface2(createSurface(kGpu_SurfaceType, context));
160 SkCanvas* canvas1 = surface1->getCanvas();
161 SkCanvas* canvas2 = surface2->getCanvas();
162 canvas1->clear(1);
163 SkAutoTUnref<SkImage> image1(surface1->newImageSnapshot());
164 // Trigger copy on write, new backing is a scratch texture
165 canvas1->clear(2);
166 SkAutoTUnref<SkImage> image2(surface1->newImageSnapshot());
167 // Trigger copy on write, old backing should not be returned to scratch
168 // pool because it is held by image2
169 canvas1->clear(3);
170
171 canvas2->clear(4);
172 SkAutoTUnref<SkImage> image3(surface2->newImageSnapshot());
173 // Trigger copy on write on surface2. The new backing store should not
174 // be recycling a texture that is held by an existing image.
175 canvas2->clear(5);
176 SkAutoTUnref<SkImage> image4(surface2->newImageSnapshot());
177 REPORTER_ASSERT(reporter, image4->getTexture() != image3->getTexture());
178 // The following assertion checks crbug.com/263329
179 REPORTER_ASSERT(reporter, image4->getTexture() != image2->getTexture());
180 REPORTER_ASSERT(reporter, image4->getTexture() != image1->getTexture());
181 REPORTER_ASSERT(reporter, image3->getTexture() != image2->getTexture());
182 REPORTER_ASSERT(reporter, image3->getTexture() != image1->getTexture());
183 REPORTER_ASSERT(reporter, image2->getTexture() != image1->getTexture());
184}
185
junov@chromium.orgda904742013-05-01 22:38:16 +0000186static void TestGetTexture(skiatest::Reporter* reporter,
187 SurfaceType surfaceType,
188 GrContext* context) {
189 SkAutoTUnref<SkSurface> surface(createSurface(surfaceType, context));
190 SkAutoTUnref<SkImage> image(surface->newImageSnapshot());
191 GrTexture* texture = image->getTexture();
192 if (surfaceType == kGpu_SurfaceType) {
193 REPORTER_ASSERT(reporter, NULL != texture);
194 REPORTER_ASSERT(reporter, 0 != texture->getTextureHandle());
195 } else {
196 REPORTER_ASSERT(reporter, NULL == texture);
197 }
198 surface->notifyContentWillChange(SkSurface::kDiscard_ContentChangeMode);
199 REPORTER_ASSERT(reporter, image->getTexture() == texture);
200}
junov@chromium.orgb516a412013-05-01 22:49:59 +0000201#endif
junov@chromium.orgda904742013-05-01 22:38:16 +0000202
junov@chromium.orgacea3ef2013-04-16 19:41:09 +0000203static void TestSurfaceNoCanvas(skiatest::Reporter* reporter,
204 SurfaceType surfaceType,
commit-bot@chromium.orgc4c98702013-04-22 14:28:01 +0000205 GrContext* context,
206 SkSurface::ContentChangeMode mode) {
junov@chromium.orgacea3ef2013-04-16 19:41:09 +0000207 // Verifies the robustness of SkSurface for handling use cases where calls
208 // are made before a canvas is created.
209 {
210 // Test passes by not asserting
211 SkSurface* surface = createSurface(surfaceType, context);
212 SkAutoTUnref<SkSurface> aur_surface(surface);
commit-bot@chromium.orgc4c98702013-04-22 14:28:01 +0000213 surface->notifyContentWillChange(mode);
robertphillips@google.com03087072013-10-02 16:42:21 +0000214 SkDEBUGCODE(surface->validate();)
junov@chromium.orgacea3ef2013-04-16 19:41:09 +0000215 }
216 {
217 SkSurface* surface = createSurface(surfaceType, context);
218 SkAutoTUnref<SkSurface> aur_surface(surface);
219 SkImage* image1 = surface->newImageSnapshot();
220 SkAutoTUnref<SkImage> aur_image1(image1);
robertphillips@google.com03087072013-10-02 16:42:21 +0000221 SkDEBUGCODE(image1->validate();)
222 SkDEBUGCODE(surface->validate();)
commit-bot@chromium.orgc4c98702013-04-22 14:28:01 +0000223 surface->notifyContentWillChange(mode);
robertphillips@google.com03087072013-10-02 16:42:21 +0000224 SkDEBUGCODE(image1->validate();)
225 SkDEBUGCODE(surface->validate();)
junov@chromium.orgacea3ef2013-04-16 19:41:09 +0000226 SkImage* image2 = surface->newImageSnapshot();
227 SkAutoTUnref<SkImage> aur_image2(image2);
robertphillips@google.com03087072013-10-02 16:42:21 +0000228 SkDEBUGCODE(image2->validate();)
229 SkDEBUGCODE(surface->validate();)
junov@chromium.orgacea3ef2013-04-16 19:41:09 +0000230 REPORTER_ASSERT(reporter, image1 != image2);
231 }
skia.committer@gmail.com45fb8b62013-04-17 07:00:56 +0000232
junov@chromium.orgacea3ef2013-04-16 19:41:09 +0000233}
junov@chromium.org995beb62013-03-28 13:49:22 +0000234
tfarina@chromium.org4ee16bf2014-01-10 22:08:27 +0000235DEF_GPUTEST(Surface, reporter, factory) {
junov@chromium.orgaf058352013-04-03 15:03:26 +0000236 TestSurfaceCopyOnWrite(reporter, kRaster_SurfaceType, NULL);
237 TestSurfaceCopyOnWrite(reporter, kPicture_SurfaceType, NULL);
238 TestSurfaceWritableAfterSnapshotRelease(reporter, kRaster_SurfaceType, NULL);
239 TestSurfaceWritableAfterSnapshotRelease(reporter, kPicture_SurfaceType, NULL);
commit-bot@chromium.orgc4c98702013-04-22 14:28:01 +0000240 TestSurfaceNoCanvas(reporter, kRaster_SurfaceType, NULL, SkSurface::kDiscard_ContentChangeMode);
241 TestSurfaceNoCanvas(reporter, kRaster_SurfaceType, NULL, SkSurface::kRetain_ContentChangeMode);
junov@chromium.orgb516a412013-05-01 22:49:59 +0000242#if SK_SUPPORT_GPU
junov@chromium.orgda904742013-05-01 22:38:16 +0000243 TestGetTexture(reporter, kRaster_SurfaceType, NULL);
244 TestGetTexture(reporter, kPicture_SurfaceType, NULL);
junov@chromium.orgaf058352013-04-03 15:03:26 +0000245 if (NULL != factory) {
246 GrContext* context = factory->get(GrContextFactory::kNative_GLContextType);
robertphillips@google.com3bddb382013-11-12 13:51:03 +0000247 if (NULL != context) {
248 Test_crbug263329(reporter, context);
249 TestSurfaceCopyOnWrite(reporter, kGpu_SurfaceType, context);
250 TestSurfaceWritableAfterSnapshotRelease(reporter, kGpu_SurfaceType, context);
251 TestSurfaceNoCanvas(reporter, kGpu_SurfaceType, context, SkSurface::kDiscard_ContentChangeMode);
252 TestSurfaceNoCanvas(reporter, kGpu_SurfaceType, context, SkSurface::kRetain_ContentChangeMode);
253 TestGetTexture(reporter, kGpu_SurfaceType, context);
254 }
junov@chromium.orgaf058352013-04-03 15:03:26 +0000255 }
junov@chromium.org995beb62013-03-28 13:49:22 +0000256#endif
257}