OLD | NEW |
---|---|
1 /* | 1 /* |
2 * Copyright 2013 Google Inc. | 2 * Copyright 2013 Google Inc. |
3 * | 3 * |
4 * Use of this source code is governed by a BSD-style license that can be | 4 * Use of this source code is governed by a BSD-style license that can be |
5 * found in the LICENSE file. | 5 * found in the LICENSE file. |
6 */ | 6 */ |
7 | 7 |
8 #include "SkCanvas.h" | 8 #include "SkCanvas.h" |
9 #include "SkData.h" | 9 #include "SkData.h" |
10 #include "SkDevice.h" | 10 #include "SkDevice.h" |
11 #include "SkImageEncoder.h" | |
12 #include "SkImage_Base.h" | 11 #include "SkImage_Base.h" |
13 #include "SkPath.h" | 12 #include "SkPath.h" |
14 #include "SkRRect.h" | 13 #include "SkRRect.h" |
15 #include "SkSurface.h" | 14 #include "SkSurface.h" |
16 #include "SkUtils.h" | 15 #include "SkUtils.h" |
17 #include "Test.h" | 16 #include "Test.h" |
18 | 17 |
19 #if SK_SUPPORT_GPU | 18 #if SK_SUPPORT_GPU |
20 #include "GrContextFactory.h" | 19 #include "GrContext.h" |
21 #include "GrTest.h" | 20 #include "GrGpu.h" |
22 #else | |
23 class GrContextFactory; | |
24 class GrContext; | |
25 #endif | 21 #endif |
26 | 22 |
27 enum SurfaceType { | 23 #include <initializer_list> |
28 kRaster_SurfaceType, | |
29 kRasterDirect_SurfaceType, | |
30 kGpu_SurfaceType, | |
31 kGpuScratch_SurfaceType, | |
32 | 24 |
33 kLastSurfaceType = kGpuScratch_SurfaceType | 25 static void release_direct_surface_storage(void* pixels, void* context) { |
34 }; | |
35 static const int kSurfaceTypeCnt = kLastSurfaceType + 1; | |
36 | |
37 static void release_storage(void* pixels, void* context) { | |
38 SkASSERT(pixels == context); | 26 SkASSERT(pixels == context); |
39 sk_free(pixels); | 27 sk_free(pixels); |
40 } | 28 } |
41 | 29 static SkSurface* create_surface(SkAlphaType at = kPremul_SkAlphaType, |
42 static SkSurface* create_surface(SurfaceType surfaceType, GrContext* context, | |
43 SkAlphaType at = kPremul_SkAlphaType, | |
44 SkImageInfo* requestedInfo = nullptr) { | 30 SkImageInfo* requestedInfo = nullptr) { |
45 const SkImageInfo info = SkImageInfo::MakeN32(10, 10, at); | 31 const SkImageInfo info = SkImageInfo::MakeN32(10, 10, at); |
46 | |
47 if (requestedInfo) { | 32 if (requestedInfo) { |
48 *requestedInfo = info; | 33 *requestedInfo = info; |
49 } | 34 } |
35 return SkSurface::NewRaster(info); | |
36 } | |
37 static SkSurface* create_direct_surface(SkAlphaType at = kPremul_SkAlphaType, | |
38 SkImageInfo* requestedInfo = nullptr) { | |
39 const SkImageInfo info = SkImageInfo::MakeN32(10, 10, at); | |
40 if (requestedInfo) { | |
41 *requestedInfo = info; | |
42 } | |
43 const size_t rowBytes = info.minRowBytes(); | |
44 void* storage = sk_malloc_throw(info.getSafeSize(rowBytes)); | |
45 return SkSurface::NewRasterDirectReleaseProc(info, storage, rowBytes, | |
46 release_direct_surface_storage, | |
47 storage); | |
48 } | |
49 #if SK_SUPPORT_GPU | |
50 static SkSurface* create_gpu_surface(GrContext* context, SkAlphaType at = kPremu l_SkAlphaType, | |
51 SkImageInfo* requestedInfo = nullptr) { | |
52 const SkImageInfo info = SkImageInfo::MakeN32(10, 10, at); | |
53 if (requestedInfo) { | |
54 *requestedInfo = info; | |
55 } | |
56 return SkSurface::NewRenderTarget(context, SkSurface::kNo_Budgeted, info, 0, nullptr); | |
57 } | |
58 static SkSurface* create_gpu_scratch_surface(GrContext* context, | |
59 SkAlphaType at = kPremul_SkAlphaTyp e, | |
60 SkImageInfo* requestedInfo = nullpt r) { | |
61 const SkImageInfo info = SkImageInfo::MakeN32(10, 10, at); | |
62 if (requestedInfo) { | |
63 *requestedInfo = info; | |
64 } | |
65 return SkSurface::NewRenderTarget(context, SkSurface::kYes_Budgeted, info, 0 , nullptr); | |
66 } | |
67 #endif | |
50 | 68 |
51 switch (surfaceType) { | 69 DEF_TEST(SurfaceEmpty, reporter) { |
52 case kRaster_SurfaceType: | |
53 return SkSurface::NewRaster(info); | |
54 case kRasterDirect_SurfaceType: { | |
55 const size_t rowBytes = info.minRowBytes(); | |
56 void* storage = sk_malloc_throw(info.getSafeSize(rowBytes)); | |
57 return SkSurface::NewRasterDirectReleaseProc(info, storage, rowBytes , | |
58 release_storage, storag e); | |
59 } | |
60 case kGpu_SurfaceType: | |
61 return SkSurface::NewRenderTarget(context, SkSurface::kNo_Budgeted, info, 0, nullptr); | |
62 case kGpuScratch_SurfaceType: | |
63 return SkSurface::NewRenderTarget(context, SkSurface::kYes_Budgeted, info, 0, nullptr); | |
64 } | |
65 return nullptr; | |
66 } | |
67 | |
68 static void test_empty_surface(skiatest::Reporter* reporter, GrContext* ctx) { | |
69 const SkImageInfo info = SkImageInfo::Make(0, 0, kN32_SkColorType, kPremul_S kAlphaType); | 70 const SkImageInfo info = SkImageInfo::Make(0, 0, kN32_SkColorType, kPremul_S kAlphaType); |
70 | |
71 REPORTER_ASSERT(reporter, nullptr == SkSurface::NewRaster(info)); | 71 REPORTER_ASSERT(reporter, nullptr == SkSurface::NewRaster(info)); |
72 REPORTER_ASSERT(reporter, nullptr == SkSurface::NewRasterDirect(info, nullpt r, 0)); | 72 REPORTER_ASSERT(reporter, nullptr == SkSurface::NewRasterDirect(info, nullpt r, 0)); |
73 if (ctx) { | 73 |
74 REPORTER_ASSERT(reporter, nullptr == | |
75 SkSurface::NewRenderTarget(ctx, SkSurface::kNo_Budgeted, info, 0, nullptr)); | |
76 } | |
77 } | 74 } |
75 #if SK_SUPPORT_GPU | |
76 DEF_GPUTEST_FOR_RENDERING_CONTEXTS(SurfaceEmpty_Gpu, reporter, context) { | |
77 const SkImageInfo info = SkImageInfo::Make(0, 0, kN32_SkColorType, kPremul_S kAlphaType); | |
78 REPORTER_ASSERT(reporter, nullptr == | |
79 SkSurface::NewRenderTarget(context, SkSurface::kNo_Budgeted, info, 0, nullptr)); | |
80 } | |
81 #endif | |
78 | 82 |
79 #if SK_SUPPORT_GPU | 83 #if SK_SUPPORT_GPU |
80 static void test_wrapped_texture_surface(skiatest::Reporter* reporter, GrContext * ctx) { | 84 DEF_GPUTEST_FOR_RENDERING_CONTEXTS(SurfaceWrappedTexture, reporter, context) { |
81 if (nullptr == ctx) { | 85 const GrGpu* gpu = context->getGpu(); |
82 return; | |
83 } | |
84 | |
85 const GrGpu* gpu = ctx->getGpu(); | |
86 if (!gpu) { | 86 if (!gpu) { |
87 return; | 87 return; |
88 } | 88 } |
89 | 89 |
90 // Test the wrapped factory for SkSurface by creating a backend texture and then wrap it in | 90 // Test the wrapped factory for SkSurface by creating a backend texture and then wrap it in |
91 // a SkSurface. | 91 // a SkSurface. |
92 static const int kW = 100; | 92 static const int kW = 100; |
93 static const int kH = 100; | 93 static const int kH = 100; |
94 static const uint32_t kOrigColor = 0xFFAABBCC; | 94 static const uint32_t kOrigColor = 0xFFAABBCC; |
95 SkAutoTArray<uint32_t> pixels(kW * kH); | 95 SkAutoTArray<uint32_t> pixels(kW * kH); |
96 sk_memset32(pixels.get(), kOrigColor, kW * kH); | 96 sk_memset32(pixels.get(), kOrigColor, kW * kH); |
97 GrBackendObject texHandle = gpu->createTestingOnlyBackendTexture(pixels.get( ), kW, kH, | 97 GrBackendObject texHandle = gpu->createTestingOnlyBackendTexture(pixels.get( ), kW, kH, |
98 kRGBA_8888_ GrPixelConfig); | 98 kRGBA_8888_ GrPixelConfig); |
99 | 99 |
100 GrBackendTextureDesc wrappedDesc; | 100 GrBackendTextureDesc wrappedDesc; |
101 wrappedDesc.fConfig = kRGBA_8888_GrPixelConfig; | 101 wrappedDesc.fConfig = kRGBA_8888_GrPixelConfig; |
102 wrappedDesc.fWidth = kW; | 102 wrappedDesc.fWidth = kW; |
103 wrappedDesc.fHeight = kH; | 103 wrappedDesc.fHeight = kH; |
104 wrappedDesc.fOrigin = kBottomLeft_GrSurfaceOrigin; | 104 wrappedDesc.fOrigin = kBottomLeft_GrSurfaceOrigin; |
105 wrappedDesc.fSampleCnt = 0; | 105 wrappedDesc.fSampleCnt = 0; |
106 wrappedDesc.fFlags = kRenderTarget_GrBackendTextureFlag; | 106 wrappedDesc.fFlags = kRenderTarget_GrBackendTextureFlag; |
107 wrappedDesc.fTextureHandle = texHandle; | 107 wrappedDesc.fTextureHandle = texHandle; |
108 | 108 |
109 SkAutoTUnref<SkSurface> surface(SkSurface::NewWrappedRenderTarget(ctx, wrapp edDesc, nullptr)); | 109 SkAutoTUnref<SkSurface> surface( |
110 SkSurface::NewWrappedRenderTarget(context, wrappedDesc, nullptr)); | |
110 REPORTER_ASSERT(reporter, surface); | 111 REPORTER_ASSERT(reporter, surface); |
111 if (surface) { | 112 if (surface) { |
112 // Validate that we can draw to the canvas and that the original texture color is preserved | 113 // Validate that we can draw to the canvas and that the original texture color is preserved |
113 // in pixels that aren't rendered to via the surface. | 114 // in pixels that aren't rendered to via the surface. |
114 SkPaint paint; | 115 SkPaint paint; |
115 static const SkColor kRectColor = ~kOrigColor | 0xFF000000; | 116 static const SkColor kRectColor = ~kOrigColor | 0xFF000000; |
116 paint.setColor(kRectColor); | 117 paint.setColor(kRectColor); |
117 surface->getCanvas()->drawRect(SkRect::MakeWH(SkIntToScalar(kW), SkIntTo Scalar(kH)/2), | 118 surface->getCanvas()->drawRect(SkRect::MakeWH(SkIntToScalar(kW), SkIntTo Scalar(kH)/2), |
118 paint); | 119 paint); |
119 SkImageInfo readInfo = SkImageInfo::MakeN32Premul(kW, kH); | 120 SkImageInfo readInfo = SkImageInfo::MakeN32Premul(kW, kH); |
(...skipping 19 matching lines...) Expand all Loading... | |
139 for (int y = kH/2; y < kH && !stop; ++y) { | 140 for (int y = kH/2; y < kH && !stop; ++y) { |
140 for (int x = 0; x < kW && !stop; ++x) { | 141 for (int x = 0; x < kW && !stop; ++x) { |
141 REPORTER_ASSERT(reporter, origColorPM == pixels[x + y * kW]); | 142 REPORTER_ASSERT(reporter, origColorPM == pixels[x + y * kW]); |
142 if (origColorPM != pixels[x + y * kW]) { | 143 if (origColorPM != pixels[x + y * kW]) { |
143 stop = true; | 144 stop = true; |
144 } | 145 } |
145 } | 146 } |
146 } | 147 } |
147 } | 148 } |
148 gpu->deleteTestingOnlyBackendTexture(texHandle); | 149 gpu->deleteTestingOnlyBackendTexture(texHandle); |
149 | |
150 } | 150 } |
151 #endif | 151 #endif |
152 | 152 |
153 | 153 static void test_canvas_peek(skiatest::Reporter* reporter, |
154 | 154 SkSurface* surface, |
155 static void test_canvaspeek(skiatest::Reporter* reporter, | 155 const SkImageInfo& requestInfo, |
156 GrContextFactory* factory) { | 156 bool expectPeekSuccess) { |
157 static const struct { | |
158 SurfaceType fType; | |
159 bool fPeekShouldSucceed; | |
160 } gRec[] = { | |
161 { kRaster_SurfaceType, true }, | |
162 { kRasterDirect_SurfaceType, true }, | |
163 #if SK_SUPPORT_GPU | |
164 { kGpu_SurfaceType, false }, | |
165 { kGpuScratch_SurfaceType, false }, | |
166 #endif | |
167 }; | |
168 | |
169 const SkColor color = SK_ColorRED; | 157 const SkColor color = SK_ColorRED; |
170 const SkPMColor pmcolor = SkPreMultiplyColor(color); | 158 const SkPMColor pmcolor = SkPreMultiplyColor(color); |
159 SkImageInfo info; | |
160 size_t rowBytes; | |
161 surface->getCanvas()->clear(color); | |
171 | 162 |
172 int cnt; | 163 const void* addr = surface->getCanvas()->peekPixels(&info, &rowBytes); |
173 #if SK_SUPPORT_GPU | 164 bool success = SkToBool(addr); |
174 cnt = GrContextFactory::kGLContextTypeCnt; | 165 REPORTER_ASSERT(reporter, expectPeekSuccess == success); |
175 #else | |
176 cnt = 1; | |
177 #endif | |
178 | 166 |
179 for (int i= 0; i < cnt; ++i) { | 167 SkImageInfo info2; |
180 GrContext* context = nullptr; | 168 size_t rb2; |
181 #if SK_SUPPORT_GPU | 169 const void* addr2 = surface->peekPixels(&info2, &rb2); |
182 GrContextFactory::GLContextType glCtxType = (GrContextFactory::GLContext Type) i; | |
183 if (!GrContextFactory::IsRenderingGLContext(glCtxType)) { | |
184 continue; | |
185 } | |
186 context = factory->get(glCtxType); | |
187 | 170 |
188 if (nullptr == context) { | 171 if (success) { |
189 continue; | 172 REPORTER_ASSERT(reporter, requestInfo == info); |
190 } | 173 REPORTER_ASSERT(reporter, requestInfo.minRowBytes() <= rowBytes); |
191 #endif | 174 REPORTER_ASSERT(reporter, pmcolor == *(const SkPMColor*)addr); |
192 for (size_t i = 0; i < SK_ARRAY_COUNT(gRec); ++i) { | |
193 SkImageInfo info, requestInfo; | |
194 size_t rowBytes; | |
195 | 175 |
196 SkAutoTUnref<SkSurface> surface(create_surface(gRec[i].fType, contex t, | 176 REPORTER_ASSERT(reporter, addr2 == addr); |
197 kPremul_SkAlphaType, &requestInfo)); | 177 REPORTER_ASSERT(reporter, info2 == info); |
198 surface->getCanvas()->clear(color); | 178 REPORTER_ASSERT(reporter, rb2 == rowBytes); |
199 | 179 } else { |
200 const void* addr = surface->getCanvas()->peekPixels(&info, &rowBytes ); | 180 REPORTER_ASSERT(reporter, nullptr == addr2); |
201 bool success = SkToBool(addr); | |
202 REPORTER_ASSERT(reporter, gRec[i].fPeekShouldSucceed == success); | |
203 | |
204 SkImageInfo info2; | |
205 size_t rb2; | |
206 const void* addr2 = surface->peekPixels(&info2, &rb2); | |
207 | |
208 if (success) { | |
209 REPORTER_ASSERT(reporter, requestInfo == info); | |
210 REPORTER_ASSERT(reporter, requestInfo.minRowBytes() <= rowBytes) ; | |
211 REPORTER_ASSERT(reporter, pmcolor == *(const SkPMColor*)addr); | |
212 | |
213 REPORTER_ASSERT(reporter, addr2 == addr); | |
214 REPORTER_ASSERT(reporter, info2 == info); | |
215 REPORTER_ASSERT(reporter, rb2 == rowBytes); | |
216 } else { | |
217 REPORTER_ASSERT(reporter, nullptr == addr2); | |
218 } | |
219 } | |
220 } | 181 } |
221 } | 182 } |
183 DEF_TEST(SurfaceCanvasPeek, reporter) { | |
184 for (auto& surface_func : { create_surface, create_direct_surface }) { | |
185 SkImageInfo requestInfo; | |
186 SkAutoTUnref<SkSurface> surface(surface_func(kPremul_SkAlphaType, &reque stInfo)); | |
187 test_canvas_peek(reporter, surface, requestInfo, true); | |
188 } | |
189 } | |
190 | |
191 #if SK_SUPPORT_GPU | |
192 DEF_GPUTEST_FOR_RENDERING_CONTEXTS(SurfaceCanvasPeek_Gpu, reporter, context) { | |
193 for (auto& surface_func : { create_gpu_surface, create_gpu_scratch_surface } ) { | |
194 SkImageInfo requestInfo; | |
195 SkAutoTUnref<SkSurface> surface(surface_func(context, kPremul_SkAlphaTyp e, &requestInfo)); | |
196 test_canvas_peek(reporter, surface, requestInfo, false); | |
197 } | |
198 } | |
199 #endif | |
222 | 200 |
223 // For compatibility with clients that still call accessBitmap(), we need to ens ure that we bump | 201 // For compatibility with clients that still call accessBitmap(), we need to ens ure that we bump |
224 // the bitmap's genID when we draw to it, else they won't know it has new values . When they are | 202 // the bitmap's genID when we draw to it, else they won't know it has new values . When they are |
225 // exclusively using surface/image, and we can hide accessBitmap from device, we can remove this | 203 // exclusively using surface/image, and we can hide accessBitmap from device, we can remove this |
226 // test. | 204 // test. |
227 static void test_accessPixels(skiatest::Reporter* reporter, GrContextFactory* fa ctory) { | 205 void test_access_pixels(skiatest::Reporter* reporter, SkSurface* surface) { |
228 static const struct { | 206 SkCanvas* canvas = surface->getCanvas(); |
229 SurfaceType fType; | 207 canvas->clear(0); |
230 bool fPeekShouldSucceed; | 208 |
231 } gRec[] = { | 209 SkBaseDevice* device = canvas->getDevice_just_for_deprecated_compatibility_t esting(); |
232 { kRaster_SurfaceType, true }, | 210 SkBitmap bm = device->accessBitmap(false); |
233 { kRasterDirect_SurfaceType, true }, | 211 uint32_t genID0 = bm.getGenerationID(); |
212 // Now we draw something, which needs to "dirty" the genID (sorta like copy- on-write) | |
213 canvas->drawColor(SK_ColorBLUE); | |
214 // Now check that we get a different genID | |
215 uint32_t genID1 = bm.getGenerationID(); | |
216 REPORTER_ASSERT(reporter, genID0 != genID1); | |
217 } | |
218 DEF_TEST(SurfaceAccessPixels, reporter) { | |
219 for (auto& surface_func : { create_surface, create_direct_surface }) { | |
220 SkAutoTUnref<SkSurface> surface(surface_func(kPremul_SkAlphaType, nullpt r)); | |
221 test_access_pixels(reporter, surface); | |
222 } | |
223 } | |
234 #if SK_SUPPORT_GPU | 224 #if SK_SUPPORT_GPU |
235 { kGpu_SurfaceType, false }, | 225 DEF_GPUTEST_FOR_RENDERING_CONTEXTS(SurfaceAccessPixels_Gpu, reporter, context) { |
236 { kGpuScratch_SurfaceType, false }, | 226 for (auto& surface_func : { create_gpu_surface, create_gpu_scratch_surface } ) { |
227 SkAutoTUnref<SkSurface> surface(surface_func(context, kPremul_SkAlphaTyp e, nullptr)); | |
228 test_access_pixels(reporter, surface); | |
229 } | |
230 } | |
237 #endif | 231 #endif |
238 }; | |
239 | |
240 int cnt; | |
241 #if SK_SUPPORT_GPU | |
242 cnt = GrContextFactory::kGLContextTypeCnt; | |
243 #else | |
244 cnt = 1; | |
245 #endif | |
246 | |
247 for (int i= 0; i < cnt; ++i) { | |
248 GrContext* context = nullptr; | |
249 #if SK_SUPPORT_GPU | |
250 GrContextFactory::GLContextType glCtxType = (GrContextFactory::GLContext Type) i; | |
251 if (!GrContextFactory::IsRenderingGLContext(glCtxType)) { | |
252 continue; | |
253 } | |
254 context = factory->get(glCtxType); | |
255 | |
256 if (nullptr == context) { | |
257 continue; | |
258 } | |
259 #endif | |
260 for (size_t j = 0; j < SK_ARRAY_COUNT(gRec); ++j) { | |
261 SkImageInfo info, requestInfo; | |
262 | |
263 SkAutoTUnref<SkSurface> surface(create_surface(gRec[j].fType, contex t, | |
264 kPremul_SkAlphaType, &requestInfo)); | |
265 SkCanvas* canvas = surface->getCanvas(); | |
266 canvas->clear(0); | |
267 | 232 |
268 SkBaseDevice* device = canvas->getDevice_just_for_deprecated_compati bility_testing(); | 233 static void test_snapshot_alphatype(skiatest::Reporter* reporter, SkSurface* sur face, |
269 SkBitmap bm = device->accessBitmap(false); | 234 bool expectOpaque) { |
270 uint32_t genID0 = bm.getGenerationID(); | 235 REPORTER_ASSERT(reporter, surface); |
271 // Now we draw something, which needs to "dirty" the genID (sorta li ke copy-on-write) | 236 if (surface) { |
272 canvas->drawColor(SK_ColorBLUE); | 237 SkAutoTUnref<SkImage> image(surface->newImageSnapshot()); |
273 // Now check that we get a different genID | 238 REPORTER_ASSERT(reporter, image); |
274 uint32_t genID1 = bm.getGenerationID(); | 239 if (image) { |
275 REPORTER_ASSERT(reporter, genID0 != genID1); | 240 REPORTER_ASSERT(reporter, image->isOpaque() == SkToBool(expectOpaque )); |
276 } | 241 } |
277 } | 242 } |
278 } | 243 } |
279 | 244 DEF_TEST(SurfaceSnapshotAlphaType, reporter) { |
280 static void test_snap_alphatype(skiatest::Reporter* reporter, GrContextFactory* factory) { | 245 for (auto& surface_func : { create_surface, create_direct_surface }) { |
281 GrContext* context = nullptr; | 246 for (auto& isOpaque : { true, false }) { |
282 #if SK_SUPPORT_GPU | 247 SkAlphaType alphaType = isOpaque ? kOpaque_SkAlphaType : kPremul_SkA lphaType; |
283 context = factory->get(GrContextFactory::kNative_GLContextType); | 248 SkAutoTUnref<SkSurface> surface(surface_func(alphaType, nullptr)); |
284 if (nullptr == context) { | 249 test_snapshot_alphatype(reporter, surface, isOpaque); |
285 return; | |
286 } | |
287 #endif | |
288 for (int opaque = 0; opaque < 2; ++opaque) { | |
289 SkAlphaType atype = SkToBool(opaque) ? kOpaque_SkAlphaType : kPremul_SkA lphaType; | |
290 for (int st = 0; st < kSurfaceTypeCnt; ++st) { | |
291 SurfaceType stype = (SurfaceType)st; | |
292 SkAutoTUnref<SkSurface> surface(create_surface(stype, context, atype )); | |
293 REPORTER_ASSERT(reporter, surface); | |
294 if (surface) { | |
295 SkAutoTUnref<SkImage> image(surface->newImageSnapshot()); | |
296 REPORTER_ASSERT(reporter, image); | |
297 if (image) { | |
298 REPORTER_ASSERT(reporter, image->isOpaque() == SkToBool(opaq ue)); | |
299 } | |
300 } | |
301 } | 250 } |
302 } | 251 } |
303 } | 252 } |
253 #if SK_SUPPORT_GPU | |
254 DEF_GPUTEST_FOR_RENDERING_CONTEXTS(SurfaceSnapshotAlphaType_Gpu, reporter, conte xt) { | |
255 for (auto& surface_func : { create_gpu_surface, create_gpu_scratch_surface } ) { | |
256 for (auto& isOpaque : {true, false}) { | |
257 SkAlphaType alphaType = isOpaque ? kOpaque_SkAlphaType : kPremul_SkA lphaType; | |
258 SkAutoTUnref<SkSurface> surface(surface_func(context, alphaType, nul lptr)); | |
259 test_snapshot_alphatype(reporter, surface, isOpaque); | |
260 } | |
261 } | |
262 } | |
263 #endif | |
304 | 264 |
305 static void test_backend_cow(skiatest::Reporter* reporter, SkSurface* surface, | 265 static GrBackendObject get_surface_backend_texture_handle( |
306 SkSurface::BackendHandleAccess mode, | 266 SkSurface* s, SkSurface::BackendHandleAccess a) { |
307 GrBackendObject (*func)(SkSurface*, SkSurface::Back endHandleAccess)) { | 267 return s->getTextureHandle(a); |
268 } | |
269 static GrBackendObject get_surface_backend_render_target_handle( | |
270 SkSurface* s, SkSurface::BackendHandleAccess a) { | |
271 GrBackendObject result; | |
272 if (!s->getRenderTargetHandle(&result, a)) { | |
273 return 0; | |
274 } | |
275 return result; | |
276 } | |
277 | |
278 static void test_backend_handle_access_copy_on_write( | |
279 skiatest::Reporter* reporter, SkSurface* surface, SkSurface::BackendHandleAc cess mode, | |
280 GrBackendObject (*func)(SkSurface*, SkSurface::BackendHandleAccess)) { | |
308 GrBackendObject obj1 = func(surface, mode); | 281 GrBackendObject obj1 = func(surface, mode); |
309 SkAutoTUnref<SkImage> snap1(surface->newImageSnapshot()); | 282 SkAutoTUnref<SkImage> snap1(surface->newImageSnapshot()); |
310 | 283 |
311 GrBackendObject obj2 = func(surface, mode); | 284 GrBackendObject obj2 = func(surface, mode); |
312 SkAutoTUnref<SkImage> snap2(surface->newImageSnapshot()); | 285 SkAutoTUnref<SkImage> snap2(surface->newImageSnapshot()); |
313 | 286 |
314 // If the access mode triggers CoW, then the backend objects should reflect it. | 287 // If the access mode triggers CoW, then the backend objects should reflect it. |
315 REPORTER_ASSERT(reporter, (obj1 == obj2) == (snap1 == snap2)); | 288 REPORTER_ASSERT(reporter, (obj1 == obj2) == (snap1 == snap2)); |
316 } | 289 } |
290 DEF_TEST(SurfaceBackendHandleAccessCopyOnWrite, reporter) { | |
291 const SkSurface::BackendHandleAccess accessModes[] = { | |
292 SkSurface::kFlushRead_BackendHandleAccess, | |
293 SkSurface::kFlushWrite_BackendHandleAccess, | |
294 SkSurface::kDiscardWrite_BackendHandleAccess, | |
295 }; | |
296 for (auto& handle_access_func : | |
297 {get_surface_backend_texture_handle, get_surface_backend_render_targ et_handle}) { | |
298 for (auto& accessMode : accessModes) { | |
299 SkAutoTUnref<SkSurface> surface(create_surface()); | |
300 test_backend_handle_access_copy_on_write(reporter, surface, accessMo de, | |
301 handle_access_func); | |
302 } | |
303 } | |
304 } | |
305 #if SK_SUPPORT_GPU | |
306 DEF_GPUTEST_FOR_RENDERING_CONTEXTS(SurfaceBackendHandleAccessCopyOnWrite_Gpu, re porter, context) { | |
bsalomon
2015/11/17 14:49:36
Should we bake the #if SK_SUPPORT_GPU into the mac
| |
307 const SkSurface::BackendHandleAccess accessModes[] = { | |
308 SkSurface::kFlushRead_BackendHandleAccess, | |
309 SkSurface::kFlushWrite_BackendHandleAccess, | |
310 SkSurface::kDiscardWrite_BackendHandleAccess, | |
311 }; | |
312 for (auto& surface_func : { create_gpu_surface, create_gpu_scratch_surface } ) { | |
313 for (auto& handle_access_func : | |
314 { get_surface_backend_texture_handle, get_surface_backend_render _target_handle}) { | |
315 for (auto& accessMode : accessModes) { | |
316 SkAutoTUnref<SkSurface> surface(surface_func(context, kPremul_Sk AlphaType, | |
317 nullptr)); | |
318 test_backend_handle_access_copy_on_write(reporter, surface, acce ssMode, | |
319 handle_access_func); | |
320 } | |
321 } | |
322 } | |
323 } | |
324 #endif | |
317 | 325 |
318 static void TestSurfaceCopyOnWrite(skiatest::Reporter* reporter, SurfaceType sur faceType, | 326 #if SK_SUPPORT_GPU |
319 GrContext* context) { | 327 // May we (soon) eliminate the need to keep testing this, by hiding the bloody d evice! |
320 // Verify that the right canvas commands trigger a copy on write | 328 static uint32_t get_legacy_gen_id(SkSurface* surface) { |
321 SkSurface* surface = create_surface(surfaceType, context); | 329 SkBaseDevice* device = |
322 SkAutoTUnref<SkSurface> aur_surface(surface); | 330 surface->getCanvas()->getDevice_just_for_deprecated_compatibility_te sting(); |
331 return device->accessBitmap(false).getGenerationID(); | |
332 } | |
333 /* | |
334 * Test legacy behavor of bumping the surface's device's bitmap's genID when we access its | |
335 * texture handle for writing. | |
336 * | |
337 * Note: this needs to be tested separately from checking newImageSnapshot, as calling that | |
338 * can also incidentally bump the genID (when a new backing surface is created) . | |
339 */ | |
340 static void test_backend_handle_gen_id( | |
341 skiatest::Reporter* reporter, SkSurface* surface, | |
342 GrBackendObject (*func)(SkSurface*, SkSurface::BackendHandleAccess)) { | |
343 const uint32_t gen0 = get_legacy_gen_id(surface); | |
344 func(surface, SkSurface::kFlushRead_BackendHandleAccess); | |
345 const uint32_t gen1 = get_legacy_gen_id(surface); | |
346 REPORTER_ASSERT(reporter, gen0 == gen1); | |
347 | |
348 func(surface, SkSurface::kFlushWrite_BackendHandleAccess); | |
349 const uint32_t gen2 = get_legacy_gen_id(surface); | |
350 REPORTER_ASSERT(reporter, gen0 != gen2); | |
351 | |
352 func(surface, SkSurface::kDiscardWrite_BackendHandleAccess); | |
353 const uint32_t gen3 = get_legacy_gen_id(surface); | |
354 REPORTER_ASSERT(reporter, gen0 != gen3); | |
355 REPORTER_ASSERT(reporter, gen2 != gen3); | |
356 } | |
357 static void test_backend_handle_unique_id( | |
358 skiatest::Reporter* reporter, SkSurface* surface, | |
359 GrBackendObject (*func)(SkSurface*, SkSurface::BackendHandleAccess)) { | |
360 SkAutoTUnref<SkImage> image0(surface->newImageSnapshot()); | |
361 GrBackendObject obj = func(surface, SkSurface::kFlushRead_BackendHandleAcces s); | |
362 REPORTER_ASSERT(reporter, obj != 0); | |
363 SkAutoTUnref<SkImage> image1(surface->newImageSnapshot()); | |
364 // just read access should not affect the snapshot | |
365 REPORTER_ASSERT(reporter, image0->uniqueID() == image1->uniqueID()); | |
366 | |
367 obj = func(surface, SkSurface::kFlushWrite_BackendHandleAccess); | |
368 REPORTER_ASSERT(reporter, obj != 0); | |
369 SkAutoTUnref<SkImage> image2(surface->newImageSnapshot()); | |
370 // expect a new image, since we claimed we would write | |
371 REPORTER_ASSERT(reporter, image0->uniqueID() != image2->uniqueID()); | |
372 | |
373 obj = func(surface, SkSurface::kDiscardWrite_BackendHandleAccess); | |
374 REPORTER_ASSERT(reporter, obj != 0); | |
375 SkAutoTUnref<SkImage> image3(surface->newImageSnapshot()); | |
376 // expect a new(er) image, since we claimed we would write | |
377 REPORTER_ASSERT(reporter, image0->uniqueID() != image3->uniqueID()); | |
378 REPORTER_ASSERT(reporter, image2->uniqueID() != image3->uniqueID()); | |
379 } | |
380 // No CPU test. | |
381 DEF_GPUTEST_FOR_RENDERING_CONTEXTS(SurfaceBackendHandleAccessIDs_Gpu, reporter, context) { | |
382 for (auto& surface_func : { create_gpu_surface, create_gpu_scratch_surface } ) { | |
383 for (auto& test_func : { test_backend_handle_unique_id, test_backend_han dle_gen_id }) { | |
384 for (auto& handle_access_func : | |
385 { get_surface_backend_texture_handle, get_surface_backend_render _target_handle}) { | |
386 SkAutoTUnref<SkSurface> surface(surface_func(context, kPremul_Sk AlphaType, | |
387 nullptr)); | |
388 test_func(reporter, surface, handle_access_func); | |
389 } | |
390 } | |
391 } | |
392 } | |
393 #endif | |
394 | |
395 // Verify that the right canvas commands trigger a copy on write. | |
396 static void test_copy_on_write(skiatest::Reporter* reporter, SkSurface* surface) { | |
323 SkCanvas* canvas = surface->getCanvas(); | 397 SkCanvas* canvas = surface->getCanvas(); |
324 | 398 |
325 const SkRect testRect = | 399 const SkRect testRect = |
326 SkRect::MakeXYWH(SkIntToScalar(0), SkIntToScalar(0), | 400 SkRect::MakeXYWH(SkIntToScalar(0), SkIntToScalar(0), |
327 SkIntToScalar(4), SkIntToScalar(5)); | 401 SkIntToScalar(4), SkIntToScalar(5)); |
328 SkPath testPath; | 402 SkPath testPath; |
329 testPath.addRect(SkRect::MakeXYWH(SkIntToScalar(0), SkIntToScalar(0), | 403 testPath.addRect(SkRect::MakeXYWH(SkIntToScalar(0), SkIntToScalar(0), |
330 SkIntToScalar(2), SkIntToScalar(1))); | 404 SkIntToScalar(2), SkIntToScalar(1))); |
331 | 405 |
332 const SkIRect testIRect = SkIRect::MakeXYWH(0, 0, 2, 1); | 406 const SkIRect testIRect = SkIRect::MakeXYWH(0, 0, 2, 1); |
(...skipping 53 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
386 EXPECT_COPY_ON_WRITE(drawPath(testPath, testPaint)) | 460 EXPECT_COPY_ON_WRITE(drawPath(testPath, testPaint)) |
387 EXPECT_COPY_ON_WRITE(drawBitmap(testBitmap, 0, 0)) | 461 EXPECT_COPY_ON_WRITE(drawBitmap(testBitmap, 0, 0)) |
388 EXPECT_COPY_ON_WRITE(drawBitmapRect(testBitmap, testRect, nullptr)) | 462 EXPECT_COPY_ON_WRITE(drawBitmapRect(testBitmap, testRect, nullptr)) |
389 EXPECT_COPY_ON_WRITE(drawBitmapNine(testBitmap, testIRect, testRect, nullptr )) | 463 EXPECT_COPY_ON_WRITE(drawBitmapNine(testBitmap, testIRect, testRect, nullptr )) |
390 EXPECT_COPY_ON_WRITE(drawSprite(testBitmap, 0, 0, nullptr)) | 464 EXPECT_COPY_ON_WRITE(drawSprite(testBitmap, 0, 0, nullptr)) |
391 EXPECT_COPY_ON_WRITE(drawText(testText.c_str(), testText.size(), 0, 1, testP aint)) | 465 EXPECT_COPY_ON_WRITE(drawText(testText.c_str(), testText.size(), 0, 1, testP aint)) |
392 EXPECT_COPY_ON_WRITE(drawPosText(testText.c_str(), testText.size(), testPoin ts2, \ | 466 EXPECT_COPY_ON_WRITE(drawPosText(testText.c_str(), testText.size(), testPoin ts2, \ |
393 testPaint)) | 467 testPaint)) |
394 EXPECT_COPY_ON_WRITE(drawTextOnPath(testText.c_str(), testText.size(), testP ath, nullptr, \ | 468 EXPECT_COPY_ON_WRITE(drawTextOnPath(testText.c_str(), testText.size(), testP ath, nullptr, \ |
395 testPaint)) | 469 testPaint)) |
396 | 470 } |
397 const SkSurface::BackendHandleAccess accessModes[] = { | 471 DEF_TEST(SurfaceCopyOnWrite, reporter) { |
398 SkSurface::kFlushRead_BackendHandleAccess, | 472 SkAutoTUnref<SkSurface> surface(create_surface()); |
399 SkSurface::kFlushWrite_BackendHandleAccess, | 473 test_copy_on_write(reporter, surface); |
400 SkSurface::kDiscardWrite_BackendHandleAccess, | 474 } |
401 }; | 475 #if SK_SUPPORT_GPU |
402 | 476 DEF_GPUTEST_FOR_RENDERING_CONTEXTS(SurfaceCopyOnWrite_Gpu, reporter, context) { |
403 for (auto access : accessModes) { | 477 for (auto& surface_func : { create_gpu_surface, create_gpu_scratch_surface } ) { |
404 test_backend_cow(reporter, surface, access, | 478 SkAutoTUnref<SkSurface> surface(surface_func(context, kPremul_SkAlphaTyp e, nullptr)); |
405 [](SkSurface* s, SkSurface::BackendHandleAccess a) -> GrBackendObject { | 479 test_copy_on_write(reporter, surface); |
406 return s->getTextureHandle(a); | |
407 }); | |
408 | |
409 test_backend_cow(reporter, surface, access, | |
410 [](SkSurface* s, SkSurface::BackendHandleAccess a) -> GrBackendObject { | |
411 GrBackendObject result; | |
412 if (!s->getRenderTargetHandle(&result, a)) { | |
413 return 0; | |
414 } | |
415 return result; | |
416 }); | |
417 } | 480 } |
418 } | 481 } |
482 #endif | |
419 | 483 |
420 static void TestSurfaceWritableAfterSnapshotRelease(skiatest::Reporter* reporter , | 484 static void test_writable_after_snapshot_release(skiatest::Reporter* reporter, |
421 SurfaceType surfaceType, | 485 SkSurface* surface) { |
422 GrContext* context) { | |
423 // This test succeeds by not triggering an assertion. | 486 // This test succeeds by not triggering an assertion. |
424 // The test verifies that the surface remains writable (usable) after | 487 // The test verifies that the surface remains writable (usable) after |
425 // acquiring and releasing a snapshot without triggering a copy on write. | 488 // acquiring and releasing a snapshot without triggering a copy on write. |
426 SkAutoTUnref<SkSurface> surface(create_surface(surfaceType, context)); | |
427 SkCanvas* canvas = surface->getCanvas(); | 489 SkCanvas* canvas = surface->getCanvas(); |
428 canvas->clear(1); | 490 canvas->clear(1); |
429 surface->newImageSnapshot()->unref(); // Create and destroy SkImage | 491 surface->newImageSnapshot()->unref(); // Create and destroy SkImage |
430 canvas->clear(2); // Must not assert internally | 492 canvas->clear(2); // Must not assert internally |
431 } | 493 } |
494 DEF_TEST(SurfaceWriteableAfterSnapshotRelease, reporter) { | |
495 SkAutoTUnref<SkSurface> surface(create_surface()); | |
496 test_writable_after_snapshot_release(reporter, surface); | |
497 } | |
498 #if SK_SUPPORT_GPU | |
499 DEF_GPUTEST_FOR_RENDERING_CONTEXTS(SurfaceWriteableAfterSnapshotRelease_Gpu, rep orter, context) { | |
500 for (auto& surface_func : { create_gpu_surface, create_gpu_scratch_surface } ) { | |
501 SkAutoTUnref<SkSurface> surface(surface_func(context, kPremul_SkAlphaTyp e, nullptr)); | |
502 test_writable_after_snapshot_release(reporter, surface); | |
503 } | |
504 } | |
505 #endif | |
432 | 506 |
433 #if SK_SUPPORT_GPU | 507 #if SK_SUPPORT_GPU |
434 static void Test_crbug263329(skiatest::Reporter* reporter, | 508 static void test_crbug263329(skiatest::Reporter* reporter, |
435 SurfaceType surfaceType, | 509 SkSurface* surface1, |
436 GrContext* context) { | 510 SkSurface* surface2) { |
437 // This is a regression test for crbug.com/263329 | 511 // This is a regression test for crbug.com/263329 |
438 // Bug was caused by onCopyOnWrite releasing the old surface texture | 512 // Bug was caused by onCopyOnWrite releasing the old surface texture |
439 // back to the scratch texture pool even though the texture is used | 513 // back to the scratch texture pool even though the texture is used |
440 // by and active SkImage_Gpu. | 514 // by and active SkImage_Gpu. |
441 SkAutoTUnref<SkSurface> surface1(create_surface(surfaceType, context)); | |
442 SkAutoTUnref<SkSurface> surface2(create_surface(surfaceType, context)); | |
443 SkCanvas* canvas1 = surface1->getCanvas(); | 515 SkCanvas* canvas1 = surface1->getCanvas(); |
444 SkCanvas* canvas2 = surface2->getCanvas(); | 516 SkCanvas* canvas2 = surface2->getCanvas(); |
445 canvas1->clear(1); | 517 canvas1->clear(1); |
446 SkAutoTUnref<SkImage> image1(surface1->newImageSnapshot()); | 518 SkAutoTUnref<SkImage> image1(surface1->newImageSnapshot()); |
447 // Trigger copy on write, new backing is a scratch texture | 519 // Trigger copy on write, new backing is a scratch texture |
448 canvas1->clear(2); | 520 canvas1->clear(2); |
449 SkAutoTUnref<SkImage> image2(surface1->newImageSnapshot()); | 521 SkAutoTUnref<SkImage> image2(surface1->newImageSnapshot()); |
450 // Trigger copy on write, old backing should not be returned to scratch | 522 // Trigger copy on write, old backing should not be returned to scratch |
451 // pool because it is held by image2 | 523 // pool because it is held by image2 |
452 canvas1->clear(3); | 524 canvas1->clear(3); |
453 | 525 |
454 canvas2->clear(4); | 526 canvas2->clear(4); |
455 SkAutoTUnref<SkImage> image3(surface2->newImageSnapshot()); | 527 SkAutoTUnref<SkImage> image3(surface2->newImageSnapshot()); |
456 // Trigger copy on write on surface2. The new backing store should not | 528 // Trigger copy on write on surface2. The new backing store should not |
457 // be recycling a texture that is held by an existing image. | 529 // be recycling a texture that is held by an existing image. |
458 canvas2->clear(5); | 530 canvas2->clear(5); |
459 SkAutoTUnref<SkImage> image4(surface2->newImageSnapshot()); | 531 SkAutoTUnref<SkImage> image4(surface2->newImageSnapshot()); |
460 REPORTER_ASSERT(reporter, as_IB(image4)->getTexture() != as_IB(image3)->getT exture()); | 532 REPORTER_ASSERT(reporter, as_IB(image4)->getTexture() != as_IB(image3)->getT exture()); |
461 // The following assertion checks crbug.com/263329 | 533 // The following assertion checks crbug.com/263329 |
462 REPORTER_ASSERT(reporter, as_IB(image4)->getTexture() != as_IB(image2)->getT exture()); | 534 REPORTER_ASSERT(reporter, as_IB(image4)->getTexture() != as_IB(image2)->getT exture()); |
463 REPORTER_ASSERT(reporter, as_IB(image4)->getTexture() != as_IB(image1)->getT exture()); | 535 REPORTER_ASSERT(reporter, as_IB(image4)->getTexture() != as_IB(image1)->getT exture()); |
464 REPORTER_ASSERT(reporter, as_IB(image3)->getTexture() != as_IB(image2)->getT exture()); | 536 REPORTER_ASSERT(reporter, as_IB(image3)->getTexture() != as_IB(image2)->getT exture()); |
465 REPORTER_ASSERT(reporter, as_IB(image3)->getTexture() != as_IB(image1)->getT exture()); | 537 REPORTER_ASSERT(reporter, as_IB(image3)->getTexture() != as_IB(image1)->getT exture()); |
466 REPORTER_ASSERT(reporter, as_IB(image2)->getTexture() != as_IB(image1)->getT exture()); | 538 REPORTER_ASSERT(reporter, as_IB(image2)->getTexture() != as_IB(image1)->getT exture()); |
467 } | 539 } |
540 DEF_GPUTEST_FOR_RENDERING_CONTEXTS(SurfaceCRBug263329_Gpu, reporter, context) { | |
541 for (auto& surface_func : { create_gpu_surface, create_gpu_scratch_surface } ) { | |
542 SkAutoTUnref<SkSurface> surface1(surface_func(context, kPremul_SkAlphaTy pe, nullptr)); | |
543 SkAutoTUnref<SkSurface> surface2(surface_func(context, kPremul_SkAlphaTy pe, nullptr)); | |
544 test_crbug263329(reporter, surface1, surface2); | |
545 } | |
546 } | |
547 #endif | |
468 | 548 |
469 static void TestGetTexture(skiatest::Reporter* reporter, | 549 DEF_TEST(SurfaceGetTexture, reporter) { |
470 SurfaceType surfaceType, | 550 SkAutoTUnref<SkSurface> surface(create_surface()); |
471 GrContext* context) { | |
472 SkAutoTUnref<SkSurface> surface(create_surface(surfaceType, context)); | |
473 SkAutoTUnref<SkImage> image(surface->newImageSnapshot()); | 551 SkAutoTUnref<SkImage> image(surface->newImageSnapshot()); |
474 GrTexture* texture = as_IB(image)->getTexture(); | 552 REPORTER_ASSERT(reporter, as_IB(image)->getTexture() == nullptr); |
475 if (surfaceType == kGpu_SurfaceType || surfaceType == kGpuScratch_SurfaceTyp e) { | 553 surface->notifyContentWillChange(SkSurface::kDiscard_ContentChangeMode); |
554 REPORTER_ASSERT(reporter, as_IB(image)->getTexture() == nullptr); | |
555 } | |
556 #if SK_SUPPORT_GPU | |
557 DEF_GPUTEST_FOR_RENDERING_CONTEXTS(SurfaceGetTexture_Gpu, reporter, context) { | |
558 for (auto& surface_func : { create_gpu_surface, create_gpu_scratch_surface } ) { | |
559 SkAutoTUnref<SkSurface> surface(surface_func(context, kPremul_SkAlphaTyp e, nullptr)); | |
560 SkAutoTUnref<SkImage> image(surface->newImageSnapshot()); | |
561 GrTexture* texture = as_IB(image)->getTexture(); | |
476 REPORTER_ASSERT(reporter, texture); | 562 REPORTER_ASSERT(reporter, texture); |
477 REPORTER_ASSERT(reporter, 0 != texture->getTextureHandle()); | 563 REPORTER_ASSERT(reporter, 0 != texture->getTextureHandle()); |
478 } else { | 564 surface->notifyContentWillChange(SkSurface::kDiscard_ContentChangeMode); |
479 REPORTER_ASSERT(reporter, nullptr == texture); | 565 REPORTER_ASSERT(reporter, as_IB(image)->getTexture() == texture); |
480 } | 566 } |
481 surface->notifyContentWillChange(SkSurface::kDiscard_ContentChangeMode); | |
482 REPORTER_ASSERT(reporter, as_IB(image)->getTexture() == texture); | |
483 } | 567 } |
568 #endif | |
484 | 569 |
570 #if SK_SUPPORT_GPU | |
485 #include "GrGpuResourcePriv.h" | 571 #include "GrGpuResourcePriv.h" |
486 #include "SkGpuDevice.h" | 572 #include "SkGpuDevice.h" |
487 #include "SkImage_Gpu.h" | 573 #include "SkImage_Gpu.h" |
488 #include "SkSurface_Gpu.h" | 574 #include "SkSurface_Gpu.h" |
489 | 575 |
490 SkSurface::Budgeted is_budgeted(SkSurface* surf) { | 576 static SkSurface::Budgeted is_budgeted(SkSurface* surf) { |
491 return ((SkSurface_Gpu*)surf)->getDevice()->accessRenderTarget()->resourcePr iv().isBudgeted() ? | 577 return ((SkSurface_Gpu*)surf)->getDevice()->accessRenderTarget()->resourcePr iv().isBudgeted() ? |
492 SkSurface::kYes_Budgeted : SkSurface::kNo_Budgeted; | 578 SkSurface::kYes_Budgeted : SkSurface::kNo_Budgeted; |
493 } | 579 } |
494 | 580 |
495 SkSurface::Budgeted is_budgeted(SkImage* image) { | 581 static SkSurface::Budgeted is_budgeted(SkImage* image) { |
496 return ((SkImage_Gpu*)image)->getTexture()->resourcePriv().isBudgeted() ? | 582 return ((SkImage_Gpu*)image)->getTexture()->resourcePriv().isBudgeted() ? |
497 SkSurface::kYes_Budgeted : SkSurface::kNo_Budgeted; | 583 SkSurface::kYes_Budgeted : SkSurface::kNo_Budgeted; |
498 } | 584 } |
499 | 585 |
500 static void test_surface_budget(skiatest::Reporter* reporter, GrContext* context ) { | 586 DEF_GPUTEST_FOR_RENDERING_CONTEXTS(SurfaceBudget, reporter, context) { |
501 SkImageInfo info = SkImageInfo::MakeN32Premul(8,8); | 587 SkImageInfo info = SkImageInfo::MakeN32Premul(8,8); |
502 for (int i = 0; i < 2; ++i) { | 588 for (int i = 0; i < 2; ++i) { |
503 SkSurface::Budgeted sbudgeted = i ? SkSurface::kYes_Budgeted : SkSurface ::kNo_Budgeted; | 589 SkSurface::Budgeted sbudgeted = i ? SkSurface::kYes_Budgeted : SkSurface ::kNo_Budgeted; |
504 for (int j = 0; j < 2; ++j) { | 590 for (int j = 0; j < 2; ++j) { |
505 SkSurface::Budgeted ibudgeted = j ? SkSurface::kYes_Budgeted : SkSur face::kNo_Budgeted; | 591 SkSurface::Budgeted ibudgeted = j ? SkSurface::kYes_Budgeted : SkSur face::kNo_Budgeted; |
506 SkAutoTUnref<SkSurface> | 592 SkAutoTUnref<SkSurface> |
507 surface(SkSurface::NewRenderTarget(context, sbudgeted, info, 0)) ; | 593 surface(SkSurface::NewRenderTarget(context, sbudgeted, info, 0)) ; |
508 SkASSERT(surface); | 594 SkASSERT(surface); |
509 REPORTER_ASSERT(reporter, sbudgeted == is_budgeted(surface)); | 595 REPORTER_ASSERT(reporter, sbudgeted == is_budgeted(surface)); |
510 | 596 |
511 SkAutoTUnref<SkImage> image(surface->newImageSnapshot(ibudgeted)); | 597 SkAutoTUnref<SkImage> image(surface->newImageSnapshot(ibudgeted)); |
512 | 598 |
513 // Initially the image shares a texture with the surface, and the su rface decides | 599 // Initially the image shares a texture with the surface, and the su rface decides |
514 // whether it is budgeted or not. | 600 // whether it is budgeted or not. |
515 REPORTER_ASSERT(reporter, sbudgeted == is_budgeted(surface)); | 601 REPORTER_ASSERT(reporter, sbudgeted == is_budgeted(surface)); |
516 REPORTER_ASSERT(reporter, sbudgeted == is_budgeted(image)); | 602 REPORTER_ASSERT(reporter, sbudgeted == is_budgeted(image)); |
517 | 603 |
518 // Now trigger copy-on-write | 604 // Now trigger copy-on-write |
519 surface->getCanvas()->clear(SK_ColorBLUE); | 605 surface->getCanvas()->clear(SK_ColorBLUE); |
520 | 606 |
521 // They don't share a texture anymore. They should each have made th eir own budget | 607 // They don't share a texture anymore. They should each have made th eir own budget |
522 // decision. | 608 // decision. |
523 REPORTER_ASSERT(reporter, sbudgeted == is_budgeted(surface)); | 609 REPORTER_ASSERT(reporter, sbudgeted == is_budgeted(surface)); |
524 REPORTER_ASSERT(reporter, ibudgeted == is_budgeted(image)); | 610 REPORTER_ASSERT(reporter, ibudgeted == is_budgeted(image)); |
525 } | 611 } |
526 } | 612 } |
527 } | 613 } |
528 | |
529 #endif | 614 #endif |
530 | 615 |
531 static void TestSurfaceNoCanvas(skiatest::Reporter* reporter, | 616 static void test_no_canvas1(skiatest::Reporter* reporter, |
532 SurfaceType surfaceType, | 617 SkSurface* surface, |
533 GrContext* context, | 618 SkSurface::ContentChangeMode mode) { |
534 SkSurface::ContentChangeMode mode) { | 619 // Test passes by not asserting |
620 surface->notifyContentWillChange(mode); | |
621 SkDEBUGCODE(surface->validate();) | |
622 } | |
623 static void test_no_canvas2(skiatest::Reporter* reporter, | |
624 SkSurface* surface, | |
625 SkSurface::ContentChangeMode mode) { | |
535 // Verifies the robustness of SkSurface for handling use cases where calls | 626 // Verifies the robustness of SkSurface for handling use cases where calls |
536 // are made before a canvas is created. | 627 // are made before a canvas is created. |
537 { | 628 SkImage* image1 = surface->newImageSnapshot(); |
538 // Test passes by not asserting | 629 SkAutoTUnref<SkImage> aur_image1(image1); |
539 SkSurface* surface = create_surface(surfaceType, context); | 630 SkDEBUGCODE(image1->validate();) |
540 SkAutoTUnref<SkSurface> aur_surface(surface); | 631 SkDEBUGCODE(surface->validate();) |
541 surface->notifyContentWillChange(mode); | 632 surface->notifyContentWillChange(mode); |
542 SkDEBUGCODE(surface->validate();) | 633 SkDEBUGCODE(image1->validate();) |
634 SkDEBUGCODE(surface->validate();) | |
635 SkImage* image2 = surface->newImageSnapshot(); | |
636 SkAutoTUnref<SkImage> aur_image2(image2); | |
637 SkDEBUGCODE(image2->validate();) | |
638 SkDEBUGCODE(surface->validate();) | |
639 REPORTER_ASSERT(reporter, image1 != image2); | |
640 } | |
641 DEF_TEST(SurfaceNoCanvas, reporter) { | |
642 SkSurface::ContentChangeMode modes[] = | |
643 { SkSurface::kDiscard_ContentChangeMode, SkSurface::kRetain_ContentC hangeMode}; | |
644 for (auto& test_func : { test_no_canvas1, test_no_canvas2 }) { | |
645 for (auto& mode : modes) { | |
646 SkAutoTUnref<SkSurface> surface(create_surface()); | |
647 test_func(reporter, surface, mode); | |
648 } | |
543 } | 649 } |
544 { | |
545 SkSurface* surface = create_surface(surfaceType, context); | |
546 SkAutoTUnref<SkSurface> aur_surface(surface); | |
547 SkImage* image1 = surface->newImageSnapshot(); | |
548 SkAutoTUnref<SkImage> aur_image1(image1); | |
549 SkDEBUGCODE(image1->validate();) | |
550 SkDEBUGCODE(surface->validate();) | |
551 surface->notifyContentWillChange(mode); | |
552 SkDEBUGCODE(image1->validate();) | |
553 SkDEBUGCODE(surface->validate();) | |
554 SkImage* image2 = surface->newImageSnapshot(); | |
555 SkAutoTUnref<SkImage> aur_image2(image2); | |
556 SkDEBUGCODE(image2->validate();) | |
557 SkDEBUGCODE(surface->validate();) | |
558 REPORTER_ASSERT(reporter, image1 != image2); | |
559 } | |
560 | |
561 } | 650 } |
562 | |
563 DEF_GPUTEST(Surface, reporter, factory) { | |
564 TestSurfaceCopyOnWrite(reporter, kRaster_SurfaceType, nullptr); | |
565 TestSurfaceWritableAfterSnapshotRelease(reporter, kRaster_SurfaceType, nullp tr); | |
566 TestSurfaceNoCanvas(reporter, kRaster_SurfaceType, nullptr, SkSurface::kDisc ard_ContentChangeMode); | |
567 TestSurfaceNoCanvas(reporter, kRaster_SurfaceType, nullptr, SkSurface::kReta in_ContentChangeMode); | |
568 | |
569 | |
570 test_empty_surface(reporter, nullptr); | |
571 | |
572 | |
573 test_canvaspeek(reporter, factory); | |
574 | |
575 test_accessPixels(reporter, factory); | |
576 | |
577 test_snap_alphatype(reporter, factory); | |
578 | |
579 #if SK_SUPPORT_GPU | 651 #if SK_SUPPORT_GPU |
580 TestGetTexture(reporter, kRaster_SurfaceType, nullptr); | 652 DEF_GPUTEST_FOR_RENDERING_CONTEXTS(SurfaceNoCanvas_Gpu, reporter, context) { |
581 if (factory) { | 653 SkSurface::ContentChangeMode modes[] = |
582 for (int i= 0; i < GrContextFactory::kGLContextTypeCnt; ++i) { | 654 { SkSurface::kDiscard_ContentChangeMode, SkSurface::kRetain_ContentC hangeMode}; |
583 GrContextFactory::GLContextType glCtxType = (GrContextFactory::GLCon textType) i; | 655 for (auto& surface_func : { create_gpu_surface, create_gpu_scratch_surface } ) { |
584 if (!GrContextFactory::IsRenderingGLContext(glCtxType)) { | 656 for (auto& test_func : { test_no_canvas1, test_no_canvas2 }) { |
585 continue; | 657 for (auto& mode : modes) { |
586 } | 658 SkAutoTUnref<SkSurface> surface( |
587 GrContext* context = factory->get(glCtxType); | 659 surface_func(context, kPremul_SkAlphaType, nullptr)); |
588 if (context) { | 660 test_func(reporter, surface, mode); |
589 Test_crbug263329(reporter, kGpu_SurfaceType, context); | |
590 Test_crbug263329(reporter, kGpuScratch_SurfaceType, context); | |
591 TestSurfaceCopyOnWrite(reporter, kGpu_SurfaceType, context); | |
592 TestSurfaceCopyOnWrite(reporter, kGpuScratch_SurfaceType, contex t); | |
593 TestSurfaceWritableAfterSnapshotRelease(reporter, kGpu_SurfaceTy pe, context); | |
594 TestSurfaceWritableAfterSnapshotRelease(reporter, kGpuScratch_Su rfaceType, context); | |
595 TestSurfaceNoCanvas(reporter, kGpu_SurfaceType, context, SkSurfa ce::kDiscard_ContentChangeMode); | |
596 TestSurfaceNoCanvas(reporter, kGpuScratch_SurfaceType, context, SkSurface::kDiscard_ContentChangeMode); | |
597 TestSurfaceNoCanvas(reporter, kGpu_SurfaceType, context, SkSurfa ce::kRetain_ContentChangeMode); | |
598 TestSurfaceNoCanvas(reporter, kGpuScratch_SurfaceType, context, SkSurface::kRetain_ContentChangeMode); | |
599 TestGetTexture(reporter, kGpu_SurfaceType, context); | |
600 TestGetTexture(reporter, kGpuScratch_SurfaceType, context); | |
601 test_empty_surface(reporter, context); | |
602 test_surface_budget(reporter, context); | |
603 test_wrapped_texture_surface(reporter, context); | |
604 } | 661 } |
605 } | 662 } |
606 } | 663 } |
664 } | |
607 #endif | 665 #endif |
608 } | |
609 | |
610 | |
OLD | NEW |