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 #if SK_SUPPORT_GPU |
| 191 DEF_GPUTEST_FOR_RENDERING_CONTEXTS(SurfaceCanvasPeek_Gpu, reporter, context) { |
| 192 for (auto& surface_func : { &create_gpu_surface, &create_gpu_scratch_surface
}) { |
| 193 SkImageInfo requestInfo; |
| 194 SkAutoTUnref<SkSurface> surface(surface_func(context, kPremul_SkAlphaTyp
e, &requestInfo)); |
| 195 test_canvas_peek(reporter, surface, requestInfo, false); |
| 196 } |
| 197 } |
| 198 #endif |
222 | 199 |
223 // For compatibility with clients that still call accessBitmap(), we need to ens
ure that we bump | 200 // 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 | 201 // 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 | 202 // exclusively using surface/image, and we can hide accessBitmap from device, we
can remove this |
226 // test. | 203 // test. |
227 static void test_accessPixels(skiatest::Reporter* reporter, GrContextFactory* fa
ctory) { | 204 void test_access_pixels(skiatest::Reporter* reporter, SkSurface* surface) { |
228 static const struct { | 205 SkCanvas* canvas = surface->getCanvas(); |
229 SurfaceType fType; | 206 canvas->clear(0); |
230 bool fPeekShouldSucceed; | 207 |
231 } gRec[] = { | 208 SkBaseDevice* device = canvas->getDevice_just_for_deprecated_compatibility_t
esting(); |
232 { kRaster_SurfaceType, true }, | 209 SkBitmap bm = device->accessBitmap(false); |
233 { kRasterDirect_SurfaceType, true }, | 210 uint32_t genID0 = bm.getGenerationID(); |
| 211 // Now we draw something, which needs to "dirty" the genID (sorta like copy-
on-write) |
| 212 canvas->drawColor(SK_ColorBLUE); |
| 213 // Now check that we get a different genID |
| 214 uint32_t genID1 = bm.getGenerationID(); |
| 215 REPORTER_ASSERT(reporter, genID0 != genID1); |
| 216 } |
| 217 DEF_TEST(SurfaceAccessPixels, reporter) { |
| 218 for (auto& surface_func : { &create_surface, &create_direct_surface }) { |
| 219 SkAutoTUnref<SkSurface> surface(surface_func(kPremul_SkAlphaType, nullpt
r)); |
| 220 test_access_pixels(reporter, surface); |
| 221 } |
| 222 } |
234 #if SK_SUPPORT_GPU | 223 #if SK_SUPPORT_GPU |
235 { kGpu_SurfaceType, false }, | 224 DEF_GPUTEST_FOR_RENDERING_CONTEXTS(SurfaceAccessPixels_Gpu, reporter, context) { |
236 { kGpuScratch_SurfaceType, false }, | 225 for (auto& surface_func : { &create_gpu_surface, &create_gpu_scratch_surface
}) { |
| 226 SkAutoTUnref<SkSurface> surface(surface_func(context, kPremul_SkAlphaTyp
e, nullptr)); |
| 227 test_access_pixels(reporter, surface); |
| 228 } |
| 229 } |
237 #endif | 230 #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 | 231 |
268 SkBaseDevice* device = canvas->getDevice_just_for_deprecated_compati
bility_testing(); | 232 static void test_snapshot_alphatype(skiatest::Reporter* reporter, SkSurface* sur
face, |
269 SkBitmap bm = device->accessBitmap(false); | 233 bool expectOpaque) { |
270 uint32_t genID0 = bm.getGenerationID(); | 234 REPORTER_ASSERT(reporter, surface); |
271 // Now we draw something, which needs to "dirty" the genID (sorta li
ke copy-on-write) | 235 if (surface) { |
272 canvas->drawColor(SK_ColorBLUE); | 236 SkAutoTUnref<SkImage> image(surface->newImageSnapshot()); |
273 // Now check that we get a different genID | 237 REPORTER_ASSERT(reporter, image); |
274 uint32_t genID1 = bm.getGenerationID(); | 238 if (image) { |
275 REPORTER_ASSERT(reporter, genID0 != genID1); | 239 REPORTER_ASSERT(reporter, image->isOpaque() == SkToBool(expectOpaque
)); |
276 } | 240 } |
277 } | 241 } |
278 } | 242 } |
279 | 243 DEF_TEST(SurfaceSnapshotAlphaType, reporter) { |
280 static void test_snap_alphatype(skiatest::Reporter* reporter, GrContextFactory*
factory) { | 244 for (auto& surface_func : { &create_surface, &create_direct_surface }) { |
281 GrContext* context = nullptr; | 245 for (auto& isOpaque : { true, false }) { |
282 #if SK_SUPPORT_GPU | 246 SkAlphaType alphaType = isOpaque ? kOpaque_SkAlphaType : kPremul_SkA
lphaType; |
283 context = factory->get(GrContextFactory::kNative_GLContextType); | 247 SkAutoTUnref<SkSurface> surface(surface_func(alphaType, nullptr)); |
284 if (nullptr == context) { | 248 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 } | 249 } |
302 } | 250 } |
303 } | 251 } |
| 252 #if SK_SUPPORT_GPU |
| 253 DEF_GPUTEST_FOR_RENDERING_CONTEXTS(SurfaceSnapshotAlphaType_Gpu, reporter, conte
xt) { |
| 254 for (auto& surface_func : { &create_gpu_surface, &create_gpu_scratch_surface
}) { |
| 255 for (auto& isOpaque : { true, false }) { |
| 256 SkAlphaType alphaType = isOpaque ? kOpaque_SkAlphaType : kPremul_SkA
lphaType; |
| 257 SkAutoTUnref<SkSurface> surface(surface_func(context, alphaType, nul
lptr)); |
| 258 test_snapshot_alphatype(reporter, surface, isOpaque); |
| 259 } |
| 260 } |
| 261 } |
| 262 #endif |
304 | 263 |
305 static void test_backend_cow(skiatest::Reporter* reporter, SkSurface* surface, | 264 static GrBackendObject get_surface_backend_texture_handle( |
306 SkSurface::BackendHandleAccess mode, | 265 SkSurface* s, SkSurface::BackendHandleAccess a) { |
307 GrBackendObject (*func)(SkSurface*, SkSurface::Back
endHandleAccess)) { | 266 return s->getTextureHandle(a); |
| 267 } |
| 268 static GrBackendObject get_surface_backend_render_target_handle( |
| 269 SkSurface* s, SkSurface::BackendHandleAccess a) { |
| 270 GrBackendObject result; |
| 271 if (!s->getRenderTargetHandle(&result, a)) { |
| 272 return 0; |
| 273 } |
| 274 return result; |
| 275 } |
| 276 |
| 277 static void test_backend_handle_access_copy_on_write( |
| 278 skiatest::Reporter* reporter, SkSurface* surface, SkSurface::BackendHandleAc
cess mode, |
| 279 GrBackendObject (*func)(SkSurface*, SkSurface::BackendHandleAccess)) { |
308 GrBackendObject obj1 = func(surface, mode); | 280 GrBackendObject obj1 = func(surface, mode); |
309 SkAutoTUnref<SkImage> snap1(surface->newImageSnapshot()); | 281 SkAutoTUnref<SkImage> snap1(surface->newImageSnapshot()); |
310 | 282 |
311 GrBackendObject obj2 = func(surface, mode); | 283 GrBackendObject obj2 = func(surface, mode); |
312 SkAutoTUnref<SkImage> snap2(surface->newImageSnapshot()); | 284 SkAutoTUnref<SkImage> snap2(surface->newImageSnapshot()); |
313 | 285 |
314 // If the access mode triggers CoW, then the backend objects should reflect
it. | 286 // If the access mode triggers CoW, then the backend objects should reflect
it. |
315 REPORTER_ASSERT(reporter, (obj1 == obj2) == (snap1 == snap2)); | 287 REPORTER_ASSERT(reporter, (obj1 == obj2) == (snap1 == snap2)); |
316 } | 288 } |
| 289 DEF_TEST(SurfaceBackendHandleAccessCopyOnWrite, reporter) { |
| 290 const SkSurface::BackendHandleAccess accessModes[] = { |
| 291 SkSurface::kFlushRead_BackendHandleAccess, |
| 292 SkSurface::kFlushWrite_BackendHandleAccess, |
| 293 SkSurface::kDiscardWrite_BackendHandleAccess, |
| 294 }; |
| 295 for (auto& handle_access_func : |
| 296 { &get_surface_backend_texture_handle, &get_surface_backend_render_t
arget_handle }) { |
| 297 for (auto& accessMode : accessModes) { |
| 298 SkAutoTUnref<SkSurface> surface(create_surface()); |
| 299 test_backend_handle_access_copy_on_write(reporter, surface, accessMo
de, |
| 300 handle_access_func); |
| 301 } |
| 302 } |
| 303 } |
| 304 #if SK_SUPPORT_GPU |
| 305 DEF_GPUTEST_FOR_RENDERING_CONTEXTS(SurfaceBackendHandleAccessCopyOnWrite_Gpu, re
porter, context) { |
| 306 const SkSurface::BackendHandleAccess accessModes[] = { |
| 307 SkSurface::kFlushRead_BackendHandleAccess, |
| 308 SkSurface::kFlushWrite_BackendHandleAccess, |
| 309 SkSurface::kDiscardWrite_BackendHandleAccess, |
| 310 }; |
| 311 for (auto& surface_func : { &create_gpu_surface, &create_gpu_scratch_surface
}) { |
| 312 for (auto& handle_access_func : |
| 313 { &get_surface_backend_texture_handle, &get_surface_backend_rend
er_target_handle }) { |
| 314 for (auto& accessMode : accessModes) { |
| 315 SkAutoTUnref<SkSurface> surface(surface_func(context, kPremul_Sk
AlphaType, |
| 316 nullptr)); |
| 317 test_backend_handle_access_copy_on_write(reporter, surface, acce
ssMode, |
| 318 handle_access_func); |
| 319 } |
| 320 } |
| 321 } |
| 322 } |
| 323 #endif |
317 | 324 |
318 static void TestSurfaceCopyOnWrite(skiatest::Reporter* reporter, SurfaceType sur
faceType, | 325 #if SK_SUPPORT_GPU |
319 GrContext* context) { | 326 // 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 | 327 static uint32_t get_legacy_gen_id(SkSurface* surface) { |
321 SkSurface* surface = create_surface(surfaceType, context); | 328 SkBaseDevice* device = |
322 SkAutoTUnref<SkSurface> aur_surface(surface); | 329 surface->getCanvas()->getDevice_just_for_deprecated_compatibility_te
sting(); |
| 330 return device->accessBitmap(false).getGenerationID(); |
| 331 } |
| 332 /* |
| 333 * Test legacy behavor of bumping the surface's device's bitmap's genID when we
access its |
| 334 * texture handle for writing. |
| 335 * |
| 336 * Note: this needs to be tested separately from checking newImageSnapshot, as
calling that |
| 337 * can also incidentally bump the genID (when a new backing surface is created)
. |
| 338 */ |
| 339 static void test_backend_handle_gen_id( |
| 340 skiatest::Reporter* reporter, SkSurface* surface, |
| 341 GrBackendObject (*func)(SkSurface*, SkSurface::BackendHandleAccess)) { |
| 342 const uint32_t gen0 = get_legacy_gen_id(surface); |
| 343 func(surface, SkSurface::kFlushRead_BackendHandleAccess); |
| 344 const uint32_t gen1 = get_legacy_gen_id(surface); |
| 345 REPORTER_ASSERT(reporter, gen0 == gen1); |
| 346 |
| 347 func(surface, SkSurface::kFlushWrite_BackendHandleAccess); |
| 348 const uint32_t gen2 = get_legacy_gen_id(surface); |
| 349 REPORTER_ASSERT(reporter, gen0 != gen2); |
| 350 |
| 351 func(surface, SkSurface::kDiscardWrite_BackendHandleAccess); |
| 352 const uint32_t gen3 = get_legacy_gen_id(surface); |
| 353 REPORTER_ASSERT(reporter, gen0 != gen3); |
| 354 REPORTER_ASSERT(reporter, gen2 != gen3); |
| 355 } |
| 356 static void test_backend_handle_unique_id( |
| 357 skiatest::Reporter* reporter, SkSurface* surface, |
| 358 GrBackendObject (*func)(SkSurface*, SkSurface::BackendHandleAccess)) { |
| 359 SkAutoTUnref<SkImage> image0(surface->newImageSnapshot()); |
| 360 GrBackendObject obj = func(surface, SkSurface::kFlushRead_BackendHandleAcces
s); |
| 361 REPORTER_ASSERT(reporter, obj != 0); |
| 362 SkAutoTUnref<SkImage> image1(surface->newImageSnapshot()); |
| 363 // just read access should not affect the snapshot |
| 364 REPORTER_ASSERT(reporter, image0->uniqueID() == image1->uniqueID()); |
| 365 |
| 366 obj = func(surface, SkSurface::kFlushWrite_BackendHandleAccess); |
| 367 REPORTER_ASSERT(reporter, obj != 0); |
| 368 SkAutoTUnref<SkImage> image2(surface->newImageSnapshot()); |
| 369 // expect a new image, since we claimed we would write |
| 370 REPORTER_ASSERT(reporter, image0->uniqueID() != image2->uniqueID()); |
| 371 |
| 372 obj = func(surface, SkSurface::kDiscardWrite_BackendHandleAccess); |
| 373 REPORTER_ASSERT(reporter, obj != 0); |
| 374 SkAutoTUnref<SkImage> image3(surface->newImageSnapshot()); |
| 375 // expect a new(er) image, since we claimed we would write |
| 376 REPORTER_ASSERT(reporter, image0->uniqueID() != image3->uniqueID()); |
| 377 REPORTER_ASSERT(reporter, image2->uniqueID() != image3->uniqueID()); |
| 378 } |
| 379 // No CPU test. |
| 380 DEF_GPUTEST_FOR_RENDERING_CONTEXTS(SurfaceBackendHandleAccessIDs_Gpu, reporter,
context) { |
| 381 for (auto& surface_func : { &create_gpu_surface, &create_gpu_scratch_surface
}) { |
| 382 for (auto& test_func : { &test_backend_handle_unique_id, &test_backend_h
andle_gen_id }) { |
| 383 for (auto& handle_access_func : |
| 384 { &get_surface_backend_texture_handle, &get_surface_backend_rend
er_target_handle}) { |
| 385 SkAutoTUnref<SkSurface> surface(surface_func(context, kPremul_Sk
AlphaType, |
| 386 nullptr)); |
| 387 test_func(reporter, surface, handle_access_func); |
| 388 } |
| 389 } |
| 390 } |
| 391 } |
| 392 #endif |
| 393 |
| 394 // Verify that the right canvas commands trigger a copy on write. |
| 395 static void test_copy_on_write(skiatest::Reporter* reporter, SkSurface* surface)
{ |
323 SkCanvas* canvas = surface->getCanvas(); | 396 SkCanvas* canvas = surface->getCanvas(); |
324 | 397 |
325 const SkRect testRect = | 398 const SkRect testRect = |
326 SkRect::MakeXYWH(SkIntToScalar(0), SkIntToScalar(0), | 399 SkRect::MakeXYWH(SkIntToScalar(0), SkIntToScalar(0), |
327 SkIntToScalar(4), SkIntToScalar(5)); | 400 SkIntToScalar(4), SkIntToScalar(5)); |
328 SkPath testPath; | 401 SkPath testPath; |
329 testPath.addRect(SkRect::MakeXYWH(SkIntToScalar(0), SkIntToScalar(0), | 402 testPath.addRect(SkRect::MakeXYWH(SkIntToScalar(0), SkIntToScalar(0), |
330 SkIntToScalar(2), SkIntToScalar(1))); | 403 SkIntToScalar(2), SkIntToScalar(1))); |
331 | 404 |
332 const SkIRect testIRect = SkIRect::MakeXYWH(0, 0, 2, 1); | 405 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)) | 459 EXPECT_COPY_ON_WRITE(drawPath(testPath, testPaint)) |
387 EXPECT_COPY_ON_WRITE(drawBitmap(testBitmap, 0, 0)) | 460 EXPECT_COPY_ON_WRITE(drawBitmap(testBitmap, 0, 0)) |
388 EXPECT_COPY_ON_WRITE(drawBitmapRect(testBitmap, testRect, nullptr)) | 461 EXPECT_COPY_ON_WRITE(drawBitmapRect(testBitmap, testRect, nullptr)) |
389 EXPECT_COPY_ON_WRITE(drawBitmapNine(testBitmap, testIRect, testRect, nullptr
)) | 462 EXPECT_COPY_ON_WRITE(drawBitmapNine(testBitmap, testIRect, testRect, nullptr
)) |
390 EXPECT_COPY_ON_WRITE(drawSprite(testBitmap, 0, 0, nullptr)) | 463 EXPECT_COPY_ON_WRITE(drawSprite(testBitmap, 0, 0, nullptr)) |
391 EXPECT_COPY_ON_WRITE(drawText(testText.c_str(), testText.size(), 0, 1, testP
aint)) | 464 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, \ | 465 EXPECT_COPY_ON_WRITE(drawPosText(testText.c_str(), testText.size(), testPoin
ts2, \ |
393 testPaint)) | 466 testPaint)) |
394 EXPECT_COPY_ON_WRITE(drawTextOnPath(testText.c_str(), testText.size(), testP
ath, nullptr, \ | 467 EXPECT_COPY_ON_WRITE(drawTextOnPath(testText.c_str(), testText.size(), testP
ath, nullptr, \ |
395 testPaint)) | 468 testPaint)) |
396 | 469 } |
397 const SkSurface::BackendHandleAccess accessModes[] = { | 470 DEF_TEST(SurfaceCopyOnWrite, reporter) { |
398 SkSurface::kFlushRead_BackendHandleAccess, | 471 SkAutoTUnref<SkSurface> surface(create_surface()); |
399 SkSurface::kFlushWrite_BackendHandleAccess, | 472 test_copy_on_write(reporter, surface); |
400 SkSurface::kDiscardWrite_BackendHandleAccess, | 473 } |
401 }; | 474 #if SK_SUPPORT_GPU |
402 | 475 DEF_GPUTEST_FOR_RENDERING_CONTEXTS(SurfaceCopyOnWrite_Gpu, reporter, context) { |
403 for (auto access : accessModes) { | 476 for (auto& surface_func : { &create_gpu_surface, &create_gpu_scratch_surface
}) { |
404 test_backend_cow(reporter, surface, access, | 477 SkAutoTUnref<SkSurface> surface(surface_func(context, kPremul_SkAlphaTyp
e, nullptr)); |
405 [](SkSurface* s, SkSurface::BackendHandleAccess a) ->
GrBackendObject { | 478 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 } | 479 } |
418 } | 480 } |
| 481 #endif |
419 | 482 |
420 static void TestSurfaceWritableAfterSnapshotRelease(skiatest::Reporter* reporter
, | 483 static void test_writable_after_snapshot_release(skiatest::Reporter* reporter, |
421 SurfaceType surfaceType, | 484 SkSurface* surface) { |
422 GrContext* context) { | |
423 // This test succeeds by not triggering an assertion. | 485 // This test succeeds by not triggering an assertion. |
424 // The test verifies that the surface remains writable (usable) after | 486 // The test verifies that the surface remains writable (usable) after |
425 // acquiring and releasing a snapshot without triggering a copy on write. | 487 // acquiring and releasing a snapshot without triggering a copy on write. |
426 SkAutoTUnref<SkSurface> surface(create_surface(surfaceType, context)); | |
427 SkCanvas* canvas = surface->getCanvas(); | 488 SkCanvas* canvas = surface->getCanvas(); |
428 canvas->clear(1); | 489 canvas->clear(1); |
429 surface->newImageSnapshot()->unref(); // Create and destroy SkImage | 490 surface->newImageSnapshot()->unref(); // Create and destroy SkImage |
430 canvas->clear(2); // Must not assert internally | 491 canvas->clear(2); // Must not assert internally |
431 } | 492 } |
| 493 DEF_TEST(SurfaceWriteableAfterSnapshotRelease, reporter) { |
| 494 SkAutoTUnref<SkSurface> surface(create_surface()); |
| 495 test_writable_after_snapshot_release(reporter, surface); |
| 496 } |
| 497 #if SK_SUPPORT_GPU |
| 498 DEF_GPUTEST_FOR_RENDERING_CONTEXTS(SurfaceWriteableAfterSnapshotRelease_Gpu, rep
orter, context) { |
| 499 for (auto& surface_func : { &create_gpu_surface, &create_gpu_scratch_surface
}) { |
| 500 SkAutoTUnref<SkSurface> surface(surface_func(context, kPremul_SkAlphaTyp
e, nullptr)); |
| 501 test_writable_after_snapshot_release(reporter, surface); |
| 502 } |
| 503 } |
| 504 #endif |
432 | 505 |
433 #if SK_SUPPORT_GPU | 506 #if SK_SUPPORT_GPU |
434 static void Test_crbug263329(skiatest::Reporter* reporter, | 507 static void test_crbug263329(skiatest::Reporter* reporter, |
435 SurfaceType surfaceType, | 508 SkSurface* surface1, |
436 GrContext* context) { | 509 SkSurface* surface2) { |
437 // This is a regression test for crbug.com/263329 | 510 // This is a regression test for crbug.com/263329 |
438 // Bug was caused by onCopyOnWrite releasing the old surface texture | 511 // Bug was caused by onCopyOnWrite releasing the old surface texture |
439 // back to the scratch texture pool even though the texture is used | 512 // back to the scratch texture pool even though the texture is used |
440 // by and active SkImage_Gpu. | 513 // 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(); | 514 SkCanvas* canvas1 = surface1->getCanvas(); |
444 SkCanvas* canvas2 = surface2->getCanvas(); | 515 SkCanvas* canvas2 = surface2->getCanvas(); |
445 canvas1->clear(1); | 516 canvas1->clear(1); |
446 SkAutoTUnref<SkImage> image1(surface1->newImageSnapshot()); | 517 SkAutoTUnref<SkImage> image1(surface1->newImageSnapshot()); |
447 // Trigger copy on write, new backing is a scratch texture | 518 // Trigger copy on write, new backing is a scratch texture |
448 canvas1->clear(2); | 519 canvas1->clear(2); |
449 SkAutoTUnref<SkImage> image2(surface1->newImageSnapshot()); | 520 SkAutoTUnref<SkImage> image2(surface1->newImageSnapshot()); |
450 // Trigger copy on write, old backing should not be returned to scratch | 521 // Trigger copy on write, old backing should not be returned to scratch |
451 // pool because it is held by image2 | 522 // pool because it is held by image2 |
452 canvas1->clear(3); | 523 canvas1->clear(3); |
453 | 524 |
454 canvas2->clear(4); | 525 canvas2->clear(4); |
455 SkAutoTUnref<SkImage> image3(surface2->newImageSnapshot()); | 526 SkAutoTUnref<SkImage> image3(surface2->newImageSnapshot()); |
456 // Trigger copy on write on surface2. The new backing store should not | 527 // Trigger copy on write on surface2. The new backing store should not |
457 // be recycling a texture that is held by an existing image. | 528 // be recycling a texture that is held by an existing image. |
458 canvas2->clear(5); | 529 canvas2->clear(5); |
459 SkAutoTUnref<SkImage> image4(surface2->newImageSnapshot()); | 530 SkAutoTUnref<SkImage> image4(surface2->newImageSnapshot()); |
460 REPORTER_ASSERT(reporter, as_IB(image4)->getTexture() != as_IB(image3)->getT
exture()); | 531 REPORTER_ASSERT(reporter, as_IB(image4)->getTexture() != as_IB(image3)->getT
exture()); |
461 // The following assertion checks crbug.com/263329 | 532 // The following assertion checks crbug.com/263329 |
462 REPORTER_ASSERT(reporter, as_IB(image4)->getTexture() != as_IB(image2)->getT
exture()); | 533 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()); | 534 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()); | 535 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()); | 536 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()); | 537 REPORTER_ASSERT(reporter, as_IB(image2)->getTexture() != as_IB(image1)->getT
exture()); |
467 } | 538 } |
| 539 DEF_GPUTEST_FOR_RENDERING_CONTEXTS(SurfaceCRBug263329_Gpu, reporter, context) { |
| 540 for (auto& surface_func : { &create_gpu_surface, &create_gpu_scratch_surface
}) { |
| 541 SkAutoTUnref<SkSurface> surface1(surface_func(context, kPremul_SkAlphaTy
pe, nullptr)); |
| 542 SkAutoTUnref<SkSurface> surface2(surface_func(context, kPremul_SkAlphaTy
pe, nullptr)); |
| 543 test_crbug263329(reporter, surface1, surface2); |
| 544 } |
| 545 } |
| 546 #endif |
468 | 547 |
469 static void TestGetTexture(skiatest::Reporter* reporter, | 548 DEF_TEST(SurfaceGetTexture, reporter) { |
470 SurfaceType surfaceType, | 549 SkAutoTUnref<SkSurface> surface(create_surface()); |
471 GrContext* context) { | |
472 SkAutoTUnref<SkSurface> surface(create_surface(surfaceType, context)); | |
473 SkAutoTUnref<SkImage> image(surface->newImageSnapshot()); | 550 SkAutoTUnref<SkImage> image(surface->newImageSnapshot()); |
474 GrTexture* texture = as_IB(image)->getTexture(); | 551 REPORTER_ASSERT(reporter, as_IB(image)->getTexture() == nullptr); |
475 if (surfaceType == kGpu_SurfaceType || surfaceType == kGpuScratch_SurfaceTyp
e) { | 552 surface->notifyContentWillChange(SkSurface::kDiscard_ContentChangeMode); |
| 553 REPORTER_ASSERT(reporter, as_IB(image)->getTexture() == nullptr); |
| 554 } |
| 555 #if SK_SUPPORT_GPU |
| 556 DEF_GPUTEST_FOR_RENDERING_CONTEXTS(SurfaceGetTexture_Gpu, reporter, context) { |
| 557 for (auto& surface_func : { &create_gpu_surface, &create_gpu_scratch_surface
}) { |
| 558 SkAutoTUnref<SkSurface> surface(surface_func(context, kPremul_SkAlphaTyp
e, nullptr)); |
| 559 SkAutoTUnref<SkImage> image(surface->newImageSnapshot()); |
| 560 GrTexture* texture = as_IB(image)->getTexture(); |
476 REPORTER_ASSERT(reporter, texture); | 561 REPORTER_ASSERT(reporter, texture); |
477 REPORTER_ASSERT(reporter, 0 != texture->getTextureHandle()); | 562 REPORTER_ASSERT(reporter, 0 != texture->getTextureHandle()); |
478 } else { | 563 surface->notifyContentWillChange(SkSurface::kDiscard_ContentChangeMode); |
479 REPORTER_ASSERT(reporter, nullptr == texture); | 564 REPORTER_ASSERT(reporter, as_IB(image)->getTexture() == texture); |
480 } | 565 } |
481 surface->notifyContentWillChange(SkSurface::kDiscard_ContentChangeMode); | |
482 REPORTER_ASSERT(reporter, as_IB(image)->getTexture() == texture); | |
483 } | 566 } |
| 567 #endif |
484 | 568 |
| 569 #if SK_SUPPORT_GPU |
485 #include "GrGpuResourcePriv.h" | 570 #include "GrGpuResourcePriv.h" |
486 #include "SkGpuDevice.h" | 571 #include "SkGpuDevice.h" |
487 #include "SkImage_Gpu.h" | 572 #include "SkImage_Gpu.h" |
488 #include "SkSurface_Gpu.h" | 573 #include "SkSurface_Gpu.h" |
489 | 574 |
490 SkSurface::Budgeted is_budgeted(SkSurface* surf) { | 575 static SkSurface::Budgeted is_budgeted(SkSurface* surf) { |
491 return ((SkSurface_Gpu*)surf)->getDevice()->accessRenderTarget()->resourcePr
iv().isBudgeted() ? | 576 return ((SkSurface_Gpu*)surf)->getDevice()->accessRenderTarget()->resourcePr
iv().isBudgeted() ? |
492 SkSurface::kYes_Budgeted : SkSurface::kNo_Budgeted; | 577 SkSurface::kYes_Budgeted : SkSurface::kNo_Budgeted; |
493 } | 578 } |
494 | 579 |
495 SkSurface::Budgeted is_budgeted(SkImage* image) { | 580 static SkSurface::Budgeted is_budgeted(SkImage* image) { |
496 return ((SkImage_Gpu*)image)->getTexture()->resourcePriv().isBudgeted() ? | 581 return ((SkImage_Gpu*)image)->getTexture()->resourcePriv().isBudgeted() ? |
497 SkSurface::kYes_Budgeted : SkSurface::kNo_Budgeted; | 582 SkSurface::kYes_Budgeted : SkSurface::kNo_Budgeted; |
498 } | 583 } |
499 | 584 |
500 static void test_surface_budget(skiatest::Reporter* reporter, GrContext* context
) { | 585 DEF_GPUTEST_FOR_RENDERING_CONTEXTS(SurfaceBudget, reporter, context) { |
501 SkImageInfo info = SkImageInfo::MakeN32Premul(8,8); | 586 SkImageInfo info = SkImageInfo::MakeN32Premul(8,8); |
502 for (int i = 0; i < 2; ++i) { | 587 for (int i = 0; i < 2; ++i) { |
503 SkSurface::Budgeted sbudgeted = i ? SkSurface::kYes_Budgeted : SkSurface
::kNo_Budgeted; | 588 SkSurface::Budgeted sbudgeted = i ? SkSurface::kYes_Budgeted : SkSurface
::kNo_Budgeted; |
504 for (int j = 0; j < 2; ++j) { | 589 for (int j = 0; j < 2; ++j) { |
505 SkSurface::Budgeted ibudgeted = j ? SkSurface::kYes_Budgeted : SkSur
face::kNo_Budgeted; | 590 SkSurface::Budgeted ibudgeted = j ? SkSurface::kYes_Budgeted : SkSur
face::kNo_Budgeted; |
506 SkAutoTUnref<SkSurface> | 591 SkAutoTUnref<SkSurface> |
507 surface(SkSurface::NewRenderTarget(context, sbudgeted, info, 0))
; | 592 surface(SkSurface::NewRenderTarget(context, sbudgeted, info, 0))
; |
508 SkASSERT(surface); | 593 SkASSERT(surface); |
509 REPORTER_ASSERT(reporter, sbudgeted == is_budgeted(surface)); | 594 REPORTER_ASSERT(reporter, sbudgeted == is_budgeted(surface)); |
510 | 595 |
511 SkAutoTUnref<SkImage> image(surface->newImageSnapshot(ibudgeted)); | 596 SkAutoTUnref<SkImage> image(surface->newImageSnapshot(ibudgeted)); |
512 | 597 |
513 // Initially the image shares a texture with the surface, and the su
rface decides | 598 // Initially the image shares a texture with the surface, and the su
rface decides |
514 // whether it is budgeted or not. | 599 // whether it is budgeted or not. |
515 REPORTER_ASSERT(reporter, sbudgeted == is_budgeted(surface)); | 600 REPORTER_ASSERT(reporter, sbudgeted == is_budgeted(surface)); |
516 REPORTER_ASSERT(reporter, sbudgeted == is_budgeted(image)); | 601 REPORTER_ASSERT(reporter, sbudgeted == is_budgeted(image)); |
517 | 602 |
518 // Now trigger copy-on-write | 603 // Now trigger copy-on-write |
519 surface->getCanvas()->clear(SK_ColorBLUE); | 604 surface->getCanvas()->clear(SK_ColorBLUE); |
520 | 605 |
521 // They don't share a texture anymore. They should each have made th
eir own budget | 606 // They don't share a texture anymore. They should each have made th
eir own budget |
522 // decision. | 607 // decision. |
523 REPORTER_ASSERT(reporter, sbudgeted == is_budgeted(surface)); | 608 REPORTER_ASSERT(reporter, sbudgeted == is_budgeted(surface)); |
524 REPORTER_ASSERT(reporter, ibudgeted == is_budgeted(image)); | 609 REPORTER_ASSERT(reporter, ibudgeted == is_budgeted(image)); |
525 } | 610 } |
526 } | 611 } |
527 } | 612 } |
528 | |
529 #endif | 613 #endif |
530 | 614 |
531 static void TestSurfaceNoCanvas(skiatest::Reporter* reporter, | 615 static void test_no_canvas1(skiatest::Reporter* reporter, |
532 SurfaceType surfaceType, | 616 SkSurface* surface, |
533 GrContext* context, | 617 SkSurface::ContentChangeMode mode) { |
534 SkSurface::ContentChangeMode mode) { | 618 // Test passes by not asserting |
| 619 surface->notifyContentWillChange(mode); |
| 620 SkDEBUGCODE(surface->validate();) |
| 621 } |
| 622 static void test_no_canvas2(skiatest::Reporter* reporter, |
| 623 SkSurface* surface, |
| 624 SkSurface::ContentChangeMode mode) { |
535 // Verifies the robustness of SkSurface for handling use cases where calls | 625 // Verifies the robustness of SkSurface for handling use cases where calls |
536 // are made before a canvas is created. | 626 // are made before a canvas is created. |
537 { | 627 SkImage* image1 = surface->newImageSnapshot(); |
538 // Test passes by not asserting | 628 SkAutoTUnref<SkImage> aur_image1(image1); |
539 SkSurface* surface = create_surface(surfaceType, context); | 629 SkDEBUGCODE(image1->validate();) |
540 SkAutoTUnref<SkSurface> aur_surface(surface); | 630 SkDEBUGCODE(surface->validate();) |
541 surface->notifyContentWillChange(mode); | 631 surface->notifyContentWillChange(mode); |
542 SkDEBUGCODE(surface->validate();) | 632 SkDEBUGCODE(image1->validate();) |
| 633 SkDEBUGCODE(surface->validate();) |
| 634 SkImage* image2 = surface->newImageSnapshot(); |
| 635 SkAutoTUnref<SkImage> aur_image2(image2); |
| 636 SkDEBUGCODE(image2->validate();) |
| 637 SkDEBUGCODE(surface->validate();) |
| 638 REPORTER_ASSERT(reporter, image1 != image2); |
| 639 } |
| 640 DEF_TEST(SurfaceNoCanvas, reporter) { |
| 641 SkSurface::ContentChangeMode modes[] = |
| 642 { SkSurface::kDiscard_ContentChangeMode, SkSurface::kRetain_ContentC
hangeMode}; |
| 643 for (auto& test_func : { &test_no_canvas1, &test_no_canvas2 }) { |
| 644 for (auto& mode : modes) { |
| 645 SkAutoTUnref<SkSurface> surface(create_surface()); |
| 646 test_func(reporter, surface, mode); |
| 647 } |
543 } | 648 } |
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 } | 649 } |
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 | 650 #if SK_SUPPORT_GPU |
580 TestGetTexture(reporter, kRaster_SurfaceType, nullptr); | 651 DEF_GPUTEST_FOR_RENDERING_CONTEXTS(SurfaceNoCanvas_Gpu, reporter, context) { |
581 if (factory) { | 652 SkSurface::ContentChangeMode modes[] = |
582 for (int i= 0; i < GrContextFactory::kGLContextTypeCnt; ++i) { | 653 { SkSurface::kDiscard_ContentChangeMode, SkSurface::kRetain_ContentC
hangeMode}; |
583 GrContextFactory::GLContextType glCtxType = (GrContextFactory::GLCon
textType) i; | 654 for (auto& surface_func : { &create_gpu_surface, &create_gpu_scratch_surface
}) { |
584 if (!GrContextFactory::IsRenderingGLContext(glCtxType)) { | 655 for (auto& test_func : { &test_no_canvas1, &test_no_canvas2 }) { |
585 continue; | 656 for (auto& mode : modes) { |
586 } | 657 SkAutoTUnref<SkSurface> surface( |
587 GrContext* context = factory->get(glCtxType); | 658 surface_func(context, kPremul_SkAlphaType, nullptr)); |
588 if (context) { | 659 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 } | 660 } |
605 } | 661 } |
606 } | 662 } |
| 663 } |
607 #endif | 664 #endif |
608 } | |
609 | |
610 | |
OLD | NEW |