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 "SkImageEncoder.h" | 10 #include "SkImageEncoder.h" |
11 #include "SkRRect.h" | 11 #include "SkRRect.h" |
12 #include "SkSurface.h" | 12 #include "SkSurface.h" |
13 #include "SkUtils.h" | 13 #include "SkUtils.h" |
14 #include "Test.h" | 14 #include "Test.h" |
15 | 15 |
16 #if SK_SUPPORT_GPU | 16 #if SK_SUPPORT_GPU |
17 #include "GrContextFactory.h" | 17 #include "GrContextFactory.h" |
18 #else | 18 #else |
19 class GrContextFactory; | 19 class GrContextFactory; |
20 class GrContext; | 20 class GrContext; |
21 #endif | 21 #endif |
22 | 22 |
23 enum SurfaceType { | 23 enum SurfaceType { |
24 kRaster_SurfaceType, | 24 kRaster_SurfaceType, |
25 kRasterDirect_SurfaceType, | |
26 kGpu_SurfaceType, | 25 kGpu_SurfaceType, |
27 kPicture_SurfaceType | 26 kPicture_SurfaceType |
28 }; | 27 }; |
29 | 28 |
30 static const int gSurfaceSize = 10; | 29 static SkSurface* createSurface(SurfaceType surfaceType, GrContext* context) { |
31 static SkPMColor gSurfaceStorage[gSurfaceSize * gSurfaceSize]; | 30 static const SkImageInfo info = SkImageInfo::MakeN32Premul(10, 10); |
32 | |
33 static SkSurface* createSurface(SurfaceType surfaceType, GrContext* context, | |
34 SkImageInfo* requestedInfo = NULL) { | |
35 static const SkImageInfo info = SkImageInfo::MakeN32Premul(gSurfaceSize, | |
36 gSurfaceSize); | |
37 | |
38 if (requestedInfo) { | |
39 *requestedInfo = info; | |
40 } | |
41 | 31 |
42 switch (surfaceType) { | 32 switch (surfaceType) { |
43 case kRaster_SurfaceType: | 33 case kRaster_SurfaceType: |
44 return SkSurface::NewRaster(info); | 34 return SkSurface::NewRaster(info); |
45 case kRasterDirect_SurfaceType: | 35 case kGpu_SurfaceType: |
46 return SkSurface::NewRasterDirect(info, gSurfaceStorage, | |
47 info.minRowBytes()); | |
48 case kGpu_SurfaceType: | |
49 #if SK_SUPPORT_GPU | 36 #if SK_SUPPORT_GPU |
50 return context ? SkSurface::NewRenderTarget(context, info) : NULL; | 37 SkASSERT(NULL != context); |
| 38 return SkSurface::NewRenderTarget(context, info); |
| 39 #else |
| 40 SkASSERT(0); |
51 #endif | 41 #endif |
52 break; | 42 case kPicture_SurfaceType: |
53 case kPicture_SurfaceType: | 43 return SkSurface::NewPicture(info.fWidth, info.fHeight); |
54 return SkSurface::NewPicture(info.fWidth, info.fHeight); | |
55 } | 44 } |
| 45 SkASSERT(0); |
56 return NULL; | 46 return NULL; |
57 } | 47 } |
58 | 48 |
59 enum ImageType { | 49 enum ImageType { |
60 kRasterCopy_ImageType, | 50 kRasterCopy_ImageType, |
61 kRasterData_ImageType, | 51 kRasterData_ImageType, |
62 kGpu_ImageType, | 52 kGpu_ImageType, |
63 kPicture_ImageType, | 53 kPicture_ImageType, |
64 kCodec_ImageType, | 54 kCodec_ImageType, |
65 }; | 55 }; |
(...skipping 54 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
120 static const struct { | 110 static const struct { |
121 ImageType fType; | 111 ImageType fType; |
122 bool fPeekShouldSucceed; | 112 bool fPeekShouldSucceed; |
123 } gRec[] = { | 113 } gRec[] = { |
124 { kRasterCopy_ImageType, true }, | 114 { kRasterCopy_ImageType, true }, |
125 { kRasterData_ImageType, true }, | 115 { kRasterData_ImageType, true }, |
126 { kGpu_ImageType, false }, | 116 { kGpu_ImageType, false }, |
127 { kPicture_ImageType, false }, | 117 { kPicture_ImageType, false }, |
128 { kCodec_ImageType, false }, | 118 { kCodec_ImageType, false }, |
129 }; | 119 }; |
130 | 120 |
131 const SkColor color = SK_ColorRED; | 121 const SkColor color = SK_ColorRED; |
132 const SkPMColor pmcolor = SkPreMultiplyColor(color); | 122 const SkPMColor pmcolor = SkPreMultiplyColor(color); |
133 | 123 |
134 for (size_t i = 0; i < SK_ARRAY_COUNT(gRec); ++i) { | 124 for (size_t i = 0; i < SK_ARRAY_COUNT(gRec); ++i) { |
135 SkImageInfo info; | 125 SkImageInfo info; |
136 size_t rowBytes; | 126 size_t rowBytes; |
137 | 127 |
138 SkAutoTUnref<SkImage> image(createImage(gRec[i].fType, NULL, color)); | 128 SkAutoTUnref<SkImage> image(createImage(gRec[i].fType, NULL, color)); |
139 if (!image.get()) { | 129 if (!image.get()) { |
140 continue; // gpu may not be enabled | 130 continue; // gpu may not be enabled |
141 } | 131 } |
142 const void* addr = image->peekPixels(&info, &rowBytes); | 132 const void* addr = image->peekPixels(&info, &rowBytes); |
143 bool success = (NULL != addr); | 133 bool success = (NULL != addr); |
144 REPORTER_ASSERT(reporter, gRec[i].fPeekShouldSucceed == success); | 134 REPORTER_ASSERT(reporter, gRec[i].fPeekShouldSucceed == success); |
145 if (success) { | 135 if (success) { |
146 REPORTER_ASSERT(reporter, 10 == info.fWidth); | 136 REPORTER_ASSERT(reporter, 10 == info.fWidth); |
147 REPORTER_ASSERT(reporter, 10 == info.fHeight); | 137 REPORTER_ASSERT(reporter, 10 == info.fHeight); |
148 REPORTER_ASSERT(reporter, kPMColor_SkColorType == info.fColorType); | 138 REPORTER_ASSERT(reporter, kPMColor_SkColorType == info.fColorType); |
149 REPORTER_ASSERT(reporter, kPremul_SkAlphaType == info.fAlphaType || | 139 REPORTER_ASSERT(reporter, kPremul_SkAlphaType == info.fAlphaType || |
150 kOpaque_SkAlphaType == info.fAlphaType); | 140 kOpaque_SkAlphaType == info.fAlphaType); |
151 REPORTER_ASSERT(reporter, info.minRowBytes() <= rowBytes); | 141 REPORTER_ASSERT(reporter, info.minRowBytes() <= rowBytes); |
152 REPORTER_ASSERT(reporter, pmcolor == *(const SkPMColor*)addr); | 142 REPORTER_ASSERT(reporter, pmcolor == *(const SkPMColor*)addr); |
153 } | 143 } |
154 } | 144 } |
155 } | 145 } |
156 | |
157 static void test_canvaspeek(skiatest::Reporter* reporter, | |
158 GrContextFactory* factory) { | |
159 static const struct { | |
160 SurfaceType fType; | |
161 bool fPeekShouldSucceed; | |
162 } gRec[] = { | |
163 { kRaster_SurfaceType, true }, | |
164 { kRasterDirect_SurfaceType, true }, | |
165 #if SK_SUPPORT_GPU | |
166 { kGpu_SurfaceType, false }, | |
167 #endif | |
168 { kPicture_SurfaceType, false }, | |
169 }; | |
170 | |
171 const SkColor color = SK_ColorRED; | |
172 const SkPMColor pmcolor = SkPreMultiplyColor(color); | |
173 | |
174 GrContext* context = NULL; | |
175 #if SK_SUPPORT_GPU | |
176 context = factory->get(GrContextFactory::kNative_GLContextType); | |
177 #endif | |
178 | |
179 for (size_t i = 0; i < SK_ARRAY_COUNT(gRec); ++i) { | |
180 SkImageInfo info, requestInfo; | |
181 size_t rowBytes; | |
182 | |
183 SkAutoTUnref<SkSurface> surface(createSurface(gRec[i].fType, context, | |
184 &requestInfo)); | |
185 surface->getCanvas()->clear(color); | |
186 | |
187 const void* addr = surface->getCanvas()->peekPixels(&info, &rowBytes); | |
188 bool success = (NULL != addr); | |
189 REPORTER_ASSERT(reporter, gRec[i].fPeekShouldSucceed == success); | |
190 | |
191 SkImageInfo info2; | |
192 size_t rb2; | |
193 const void* addr2 = surface->peekPixels(&info2, &rb2); | |
194 | |
195 if (success) { | |
196 REPORTER_ASSERT(reporter, requestInfo == info); | |
197 REPORTER_ASSERT(reporter, requestInfo.minRowBytes() <= rowBytes); | |
198 REPORTER_ASSERT(reporter, pmcolor == *(const SkPMColor*)addr); | |
199 | |
200 REPORTER_ASSERT(reporter, addr2 == addr); | |
201 REPORTER_ASSERT(reporter, info2 == info); | |
202 REPORTER_ASSERT(reporter, rb2 == rowBytes); | |
203 } else { | |
204 REPORTER_ASSERT(reporter, NULL == addr2); | |
205 } | |
206 } | |
207 } | |
208 | 146 |
209 static void TestSurfaceCopyOnWrite(skiatest::Reporter* reporter, SurfaceType sur
faceType, | 147 static void TestSurfaceCopyOnWrite(skiatest::Reporter* reporter, SurfaceType sur
faceType, |
210 GrContext* context) { | 148 GrContext* context) { |
211 // Verify that the right canvas commands trigger a copy on write | 149 // Verify that the right canvas commands trigger a copy on write |
212 SkSurface* surface = createSurface(surfaceType, context); | 150 SkSurface* surface = createSurface(surfaceType, context); |
213 SkAutoTUnref<SkSurface> aur_surface(surface); | 151 SkAutoTUnref<SkSurface> aur_surface(surface); |
214 SkCanvas* canvas = surface->getCanvas(); | 152 SkCanvas* canvas = surface->getCanvas(); |
215 | 153 |
216 const SkRect testRect = | 154 const SkRect testRect = |
217 SkRect::MakeXYWH(SkIntToScalar(0), SkIntToScalar(0), | 155 SkRect::MakeXYWH(SkIntToScalar(0), SkIntToScalar(0), |
(...skipping 172 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
390 | 328 |
391 DEF_GPUTEST(Surface, reporter, factory) { | 329 DEF_GPUTEST(Surface, reporter, factory) { |
392 test_image(reporter); | 330 test_image(reporter); |
393 | 331 |
394 TestSurfaceCopyOnWrite(reporter, kRaster_SurfaceType, NULL); | 332 TestSurfaceCopyOnWrite(reporter, kRaster_SurfaceType, NULL); |
395 TestSurfaceCopyOnWrite(reporter, kPicture_SurfaceType, NULL); | 333 TestSurfaceCopyOnWrite(reporter, kPicture_SurfaceType, NULL); |
396 TestSurfaceWritableAfterSnapshotRelease(reporter, kRaster_SurfaceType, NULL)
; | 334 TestSurfaceWritableAfterSnapshotRelease(reporter, kRaster_SurfaceType, NULL)
; |
397 TestSurfaceWritableAfterSnapshotRelease(reporter, kPicture_SurfaceType, NULL
); | 335 TestSurfaceWritableAfterSnapshotRelease(reporter, kPicture_SurfaceType, NULL
); |
398 TestSurfaceNoCanvas(reporter, kRaster_SurfaceType, NULL, SkSurface::kDiscard
_ContentChangeMode); | 336 TestSurfaceNoCanvas(reporter, kRaster_SurfaceType, NULL, SkSurface::kDiscard
_ContentChangeMode); |
399 TestSurfaceNoCanvas(reporter, kRaster_SurfaceType, NULL, SkSurface::kRetain_
ContentChangeMode); | 337 TestSurfaceNoCanvas(reporter, kRaster_SurfaceType, NULL, SkSurface::kRetain_
ContentChangeMode); |
400 | |
401 test_imagepeek(reporter); | 338 test_imagepeek(reporter); |
402 test_canvaspeek(reporter, factory); | |
403 | |
404 #if SK_SUPPORT_GPU | 339 #if SK_SUPPORT_GPU |
405 TestGetTexture(reporter, kRaster_SurfaceType, NULL); | 340 TestGetTexture(reporter, kRaster_SurfaceType, NULL); |
406 TestGetTexture(reporter, kPicture_SurfaceType, NULL); | 341 TestGetTexture(reporter, kPicture_SurfaceType, NULL); |
407 if (NULL != factory) { | 342 if (NULL != factory) { |
408 GrContext* context = factory->get(GrContextFactory::kNative_GLContextTyp
e); | 343 GrContext* context = factory->get(GrContextFactory::kNative_GLContextTyp
e); |
409 if (NULL != context) { | 344 if (NULL != context) { |
410 Test_crbug263329(reporter, context); | 345 Test_crbug263329(reporter, context); |
411 TestSurfaceCopyOnWrite(reporter, kGpu_SurfaceType, context); | 346 TestSurfaceCopyOnWrite(reporter, kGpu_SurfaceType, context); |
412 TestSurfaceWritableAfterSnapshotRelease(reporter, kGpu_SurfaceType,
context); | 347 TestSurfaceWritableAfterSnapshotRelease(reporter, kGpu_SurfaceType,
context); |
413 TestSurfaceNoCanvas(reporter, kGpu_SurfaceType, context, SkSurface::
kDiscard_ContentChangeMode); | 348 TestSurfaceNoCanvas(reporter, kGpu_SurfaceType, context, SkSurface::
kDiscard_ContentChangeMode); |
414 TestSurfaceNoCanvas(reporter, kGpu_SurfaceType, context, SkSurface::
kRetain_ContentChangeMode); | 349 TestSurfaceNoCanvas(reporter, kGpu_SurfaceType, context, SkSurface::
kRetain_ContentChangeMode); |
415 TestGetTexture(reporter, kGpu_SurfaceType, context); | 350 TestGetTexture(reporter, kGpu_SurfaceType, context); |
416 } | 351 } |
417 } | 352 } |
418 #endif | 353 #endif |
419 } | 354 } |
OLD | NEW |