blob: 20e75059664d254ca3c9138e699dc0fbbaea7e32 [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"
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) {
reed@google.com2bd8b812013-11-01 13:46:54 +000027 static const SkImageInfo imageSpec = {
junov@chromium.org995beb62013-03-28 13:49:22 +000028 10, // width
29 10, // height
reed@google.com2bd8b812013-11-01 13:46:54 +000030 kPMColor_SkColorType,
reed@google.comd28ba802013-09-20 19:33:52 +000031 kPremul_SkAlphaType
junov@chromium.org995beb62013-03-28 13:49:22 +000032 };
33
34 switch (surfaceType) {
35 case kRaster_SurfaceType:
36 return SkSurface::NewRaster(imageSpec);
37 case kGpu_SurfaceType:
junov@chromium.org8bc9edc2013-04-03 15:25:46 +000038#if SK_SUPPORT_GPU
junov@chromium.org995beb62013-03-28 13:49:22 +000039 SkASSERT(NULL != context);
40 return SkSurface::NewRenderTarget(context, imageSpec);
junov@chromium.org8bc9edc2013-04-03 15:25:46 +000041#else
42 SkASSERT(0);
43#endif
junov@chromium.org995beb62013-03-28 13:49:22 +000044 case kPicture_SurfaceType:
45 return SkSurface::NewPicture(10, 10);
46 }
47 SkASSERT(0);
48 return NULL;
49}
50
reed@google.com999da9c2014-02-06 13:43:07 +000051#include "SkData.h"
52
53static void test_image(skiatest::Reporter* reporter) {
54 SkImageInfo info = SkImageInfo::MakeN32Premul(1, 1);
55 size_t rowBytes = info.minRowBytes();
56 size_t size = info.getSafeSize(rowBytes);
57 void* addr = sk_malloc_throw(size);
58 SkData* data = SkData::NewFromMalloc(addr, size);
59
60 REPORTER_ASSERT(reporter, 1 == data->getRefCnt());
61 SkImage* image = SkImage::NewRasterData(info, data, rowBytes);
62 REPORTER_ASSERT(reporter, 2 == data->getRefCnt());
63 image->unref();
64 REPORTER_ASSERT(reporter, 1 == data->getRefCnt());
65 data->unref();
66}
67
junov@chromium.org995beb62013-03-28 13:49:22 +000068static void TestSurfaceCopyOnWrite(skiatest::Reporter* reporter, SurfaceType surfaceType,
69 GrContext* context) {
70 // Verify that the right canvas commands trigger a copy on write
71 SkSurface* surface = createSurface(surfaceType, context);
72 SkAutoTUnref<SkSurface> aur_surface(surface);
73 SkCanvas* canvas = surface->getCanvas();
74
75 const SkRect testRect =
76 SkRect::MakeXYWH(SkIntToScalar(0), SkIntToScalar(0),
77 SkIntToScalar(4), SkIntToScalar(5));
78 SkMatrix testMatrix;
79 testMatrix.reset();
80 testMatrix.setScale(SkIntToScalar(2), SkIntToScalar(3));
81
82 SkPath testPath;
83 testPath.addRect(SkRect::MakeXYWH(SkIntToScalar(0), SkIntToScalar(0),
84 SkIntToScalar(2), SkIntToScalar(1)));
85
86 const SkIRect testIRect = SkIRect::MakeXYWH(0, 0, 2, 1);
87
88 SkRegion testRegion;
89 testRegion.setRect(testIRect);
90
91
92 const SkColor testColor = 0x01020304;
93 const SkPaint testPaint;
94 const SkPoint testPoints[3] = {
95 {SkIntToScalar(0), SkIntToScalar(0)},
96 {SkIntToScalar(2), SkIntToScalar(1)},
97 {SkIntToScalar(0), SkIntToScalar(2)}
98 };
99 const size_t testPointCount = 3;
100
101 SkBitmap testBitmap;
102 testBitmap.setConfig(SkBitmap::kARGB_8888_Config, 10, 10);
103 testBitmap.allocPixels();
robertphillips@google.comd1ce77d2013-10-09 12:51:09 +0000104 testBitmap.eraseColor(0);
junov@chromium.org995beb62013-03-28 13:49:22 +0000105
106 SkRRect testRRect;
107 testRRect.setRectXY(testRect, SK_Scalar1, SK_Scalar1);
108
109 SkString testText("Hello World");
110 const SkPoint testPoints2[] = {
111 { SkIntToScalar(0), SkIntToScalar(1) },
112 { SkIntToScalar(1), SkIntToScalar(1) },
113 { SkIntToScalar(2), SkIntToScalar(1) },
114 { SkIntToScalar(3), SkIntToScalar(1) },
115 { SkIntToScalar(4), SkIntToScalar(1) },
116 { SkIntToScalar(5), SkIntToScalar(1) },
117 { SkIntToScalar(6), SkIntToScalar(1) },
118 { SkIntToScalar(7), SkIntToScalar(1) },
119 { SkIntToScalar(8), SkIntToScalar(1) },
120 { SkIntToScalar(9), SkIntToScalar(1) },
121 { SkIntToScalar(10), SkIntToScalar(1) },
122 };
123
124#define EXPECT_COPY_ON_WRITE(command) \
125 { \
junov@chromium.org5ee449a2013-04-12 20:20:50 +0000126 SkImage* imageBefore = surface->newImageSnapshot(); \
junov@chromium.org995beb62013-03-28 13:49:22 +0000127 SkAutoTUnref<SkImage> aur_before(imageBefore); \
128 canvas-> command ; \
junov@chromium.org5ee449a2013-04-12 20:20:50 +0000129 SkImage* imageAfter = surface->newImageSnapshot(); \
junov@chromium.org995beb62013-03-28 13:49:22 +0000130 SkAutoTUnref<SkImage> aur_after(imageAfter); \
131 REPORTER_ASSERT(reporter, imageBefore != imageAfter); \
132 }
133
134 EXPECT_COPY_ON_WRITE(clear(testColor))
135 EXPECT_COPY_ON_WRITE(drawPaint(testPaint))
136 EXPECT_COPY_ON_WRITE(drawPoints(SkCanvas::kPoints_PointMode, testPointCount, testPoints, \
137 testPaint))
138 EXPECT_COPY_ON_WRITE(drawOval(testRect, testPaint))
139 EXPECT_COPY_ON_WRITE(drawRect(testRect, testPaint))
140 EXPECT_COPY_ON_WRITE(drawRRect(testRRect, testPaint))
141 EXPECT_COPY_ON_WRITE(drawPath(testPath, testPaint))
142 EXPECT_COPY_ON_WRITE(drawBitmap(testBitmap, 0, 0))
143 EXPECT_COPY_ON_WRITE(drawBitmapRect(testBitmap, NULL, testRect))
144 EXPECT_COPY_ON_WRITE(drawBitmapMatrix(testBitmap, testMatrix, NULL))
145 EXPECT_COPY_ON_WRITE(drawBitmapNine(testBitmap, testIRect, testRect, NULL))
146 EXPECT_COPY_ON_WRITE(drawSprite(testBitmap, 0, 0, NULL))
147 EXPECT_COPY_ON_WRITE(drawText(testText.c_str(), testText.size(), 0, 1, testPaint))
148 EXPECT_COPY_ON_WRITE(drawPosText(testText.c_str(), testText.size(), testPoints2, \
149 testPaint))
150 EXPECT_COPY_ON_WRITE(drawTextOnPath(testText.c_str(), testText.size(), testPath, NULL, \
151 testPaint))
152}
153
junov@chromium.orgaf058352013-04-03 15:03:26 +0000154static void TestSurfaceWritableAfterSnapshotRelease(skiatest::Reporter* reporter,
155 SurfaceType surfaceType,
156 GrContext* context) {
157 // This test succeeds by not triggering an assertion.
158 // The test verifies that the surface remains writable (usable) after
159 // acquiring and releasing a snapshot without triggering a copy on write.
commit-bot@chromium.org4d24b742013-07-25 23:29:40 +0000160 SkAutoTUnref<SkSurface> surface(createSurface(surfaceType, context));
junov@chromium.orgaf058352013-04-03 15:03:26 +0000161 SkCanvas* canvas = surface->getCanvas();
162 canvas->clear(1);
junov@chromium.org5ee449a2013-04-12 20:20:50 +0000163 surface->newImageSnapshot()->unref(); // Create and destroy SkImage
commit-bot@chromium.org4d24b742013-07-25 23:29:40 +0000164 canvas->clear(2); // Must not assert internally
junov@chromium.org995beb62013-03-28 13:49:22 +0000165}
junov@chromium.orgda904742013-05-01 22:38:16 +0000166
junov@chromium.orgb516a412013-05-01 22:49:59 +0000167#if SK_SUPPORT_GPU
commit-bot@chromium.org4d24b742013-07-25 23:29:40 +0000168static void Test_crbug263329(skiatest::Reporter* reporter,
169 GrContext* context) {
170 // This is a regression test for crbug.com/263329
171 // Bug was caused by onCopyOnWrite releasing the old surface texture
172 // back to the scratch texture pool even though the texture is used
173 // by and active SkImage_Gpu.
174 SkAutoTUnref<SkSurface> surface1(createSurface(kGpu_SurfaceType, context));
175 SkAutoTUnref<SkSurface> surface2(createSurface(kGpu_SurfaceType, context));
176 SkCanvas* canvas1 = surface1->getCanvas();
177 SkCanvas* canvas2 = surface2->getCanvas();
178 canvas1->clear(1);
179 SkAutoTUnref<SkImage> image1(surface1->newImageSnapshot());
180 // Trigger copy on write, new backing is a scratch texture
181 canvas1->clear(2);
182 SkAutoTUnref<SkImage> image2(surface1->newImageSnapshot());
183 // Trigger copy on write, old backing should not be returned to scratch
184 // pool because it is held by image2
185 canvas1->clear(3);
186
187 canvas2->clear(4);
188 SkAutoTUnref<SkImage> image3(surface2->newImageSnapshot());
189 // Trigger copy on write on surface2. The new backing store should not
190 // be recycling a texture that is held by an existing image.
191 canvas2->clear(5);
192 SkAutoTUnref<SkImage> image4(surface2->newImageSnapshot());
193 REPORTER_ASSERT(reporter, image4->getTexture() != image3->getTexture());
194 // The following assertion checks crbug.com/263329
195 REPORTER_ASSERT(reporter, image4->getTexture() != image2->getTexture());
196 REPORTER_ASSERT(reporter, image4->getTexture() != image1->getTexture());
197 REPORTER_ASSERT(reporter, image3->getTexture() != image2->getTexture());
198 REPORTER_ASSERT(reporter, image3->getTexture() != image1->getTexture());
199 REPORTER_ASSERT(reporter, image2->getTexture() != image1->getTexture());
200}
201
junov@chromium.orgda904742013-05-01 22:38:16 +0000202static void TestGetTexture(skiatest::Reporter* reporter,
203 SurfaceType surfaceType,
204 GrContext* context) {
205 SkAutoTUnref<SkSurface> surface(createSurface(surfaceType, context));
206 SkAutoTUnref<SkImage> image(surface->newImageSnapshot());
207 GrTexture* texture = image->getTexture();
208 if (surfaceType == kGpu_SurfaceType) {
209 REPORTER_ASSERT(reporter, NULL != texture);
210 REPORTER_ASSERT(reporter, 0 != texture->getTextureHandle());
211 } else {
212 REPORTER_ASSERT(reporter, NULL == texture);
213 }
214 surface->notifyContentWillChange(SkSurface::kDiscard_ContentChangeMode);
215 REPORTER_ASSERT(reporter, image->getTexture() == texture);
216}
junov@chromium.orgb516a412013-05-01 22:49:59 +0000217#endif
junov@chromium.orgda904742013-05-01 22:38:16 +0000218
junov@chromium.orgacea3ef2013-04-16 19:41:09 +0000219static void TestSurfaceNoCanvas(skiatest::Reporter* reporter,
220 SurfaceType surfaceType,
commit-bot@chromium.orgc4c98702013-04-22 14:28:01 +0000221 GrContext* context,
222 SkSurface::ContentChangeMode mode) {
junov@chromium.orgacea3ef2013-04-16 19:41:09 +0000223 // Verifies the robustness of SkSurface for handling use cases where calls
224 // are made before a canvas is created.
225 {
226 // Test passes by not asserting
227 SkSurface* surface = createSurface(surfaceType, context);
228 SkAutoTUnref<SkSurface> aur_surface(surface);
commit-bot@chromium.orgc4c98702013-04-22 14:28:01 +0000229 surface->notifyContentWillChange(mode);
robertphillips@google.com03087072013-10-02 16:42:21 +0000230 SkDEBUGCODE(surface->validate();)
junov@chromium.orgacea3ef2013-04-16 19:41:09 +0000231 }
232 {
233 SkSurface* surface = createSurface(surfaceType, context);
234 SkAutoTUnref<SkSurface> aur_surface(surface);
235 SkImage* image1 = surface->newImageSnapshot();
236 SkAutoTUnref<SkImage> aur_image1(image1);
robertphillips@google.com03087072013-10-02 16:42:21 +0000237 SkDEBUGCODE(image1->validate();)
238 SkDEBUGCODE(surface->validate();)
commit-bot@chromium.orgc4c98702013-04-22 14:28:01 +0000239 surface->notifyContentWillChange(mode);
robertphillips@google.com03087072013-10-02 16:42:21 +0000240 SkDEBUGCODE(image1->validate();)
241 SkDEBUGCODE(surface->validate();)
junov@chromium.orgacea3ef2013-04-16 19:41:09 +0000242 SkImage* image2 = surface->newImageSnapshot();
243 SkAutoTUnref<SkImage> aur_image2(image2);
robertphillips@google.com03087072013-10-02 16:42:21 +0000244 SkDEBUGCODE(image2->validate();)
245 SkDEBUGCODE(surface->validate();)
junov@chromium.orgacea3ef2013-04-16 19:41:09 +0000246 REPORTER_ASSERT(reporter, image1 != image2);
247 }
skia.committer@gmail.com45fb8b62013-04-17 07:00:56 +0000248
junov@chromium.orgacea3ef2013-04-16 19:41:09 +0000249}
junov@chromium.org995beb62013-03-28 13:49:22 +0000250
tfarina@chromium.org4ee16bf2014-01-10 22:08:27 +0000251DEF_GPUTEST(Surface, reporter, factory) {
reed@google.com999da9c2014-02-06 13:43:07 +0000252 test_image(reporter);
253
junov@chromium.orgaf058352013-04-03 15:03:26 +0000254 TestSurfaceCopyOnWrite(reporter, kRaster_SurfaceType, NULL);
255 TestSurfaceCopyOnWrite(reporter, kPicture_SurfaceType, NULL);
256 TestSurfaceWritableAfterSnapshotRelease(reporter, kRaster_SurfaceType, NULL);
257 TestSurfaceWritableAfterSnapshotRelease(reporter, kPicture_SurfaceType, NULL);
commit-bot@chromium.orgc4c98702013-04-22 14:28:01 +0000258 TestSurfaceNoCanvas(reporter, kRaster_SurfaceType, NULL, SkSurface::kDiscard_ContentChangeMode);
259 TestSurfaceNoCanvas(reporter, kRaster_SurfaceType, NULL, SkSurface::kRetain_ContentChangeMode);
junov@chromium.orgb516a412013-05-01 22:49:59 +0000260#if SK_SUPPORT_GPU
junov@chromium.orgda904742013-05-01 22:38:16 +0000261 TestGetTexture(reporter, kRaster_SurfaceType, NULL);
262 TestGetTexture(reporter, kPicture_SurfaceType, NULL);
junov@chromium.orgaf058352013-04-03 15:03:26 +0000263 if (NULL != factory) {
264 GrContext* context = factory->get(GrContextFactory::kNative_GLContextType);
robertphillips@google.com3bddb382013-11-12 13:51:03 +0000265 if (NULL != context) {
266 Test_crbug263329(reporter, context);
267 TestSurfaceCopyOnWrite(reporter, kGpu_SurfaceType, context);
268 TestSurfaceWritableAfterSnapshotRelease(reporter, kGpu_SurfaceType, context);
269 TestSurfaceNoCanvas(reporter, kGpu_SurfaceType, context, SkSurface::kDiscard_ContentChangeMode);
270 TestSurfaceNoCanvas(reporter, kGpu_SurfaceType, context, SkSurface::kRetain_ContentChangeMode);
271 TestGetTexture(reporter, kGpu_SurfaceType, context);
272 }
junov@chromium.orgaf058352013-04-03 15:03:26 +0000273 }
junov@chromium.org995beb62013-03-28 13:49:22 +0000274#endif
275}