Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(504)

Side by Side Diff: tests/SurfaceTest.cpp

Issue 161733002: add peekPixels to SkCanvas and SkSurface (Closed) Base URL: https://skia.googlecode.com/svn/trunk
Patch Set: Created 6 years, 10 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
« no previous file with comments | « tests/SkpSkGrTest.cpp ('k') | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
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,
25 kGpu_SurfaceType, 26 kGpu_SurfaceType,
26 kPicture_SurfaceType 27 kPicture_SurfaceType
27 }; 28 };
28 29
29 static SkSurface* createSurface(SurfaceType surfaceType, GrContext* context) { 30 static const int gSurfaceSize = 10;
30 static const SkImageInfo info = SkImageInfo::MakeN32Premul(10, 10); 31 static SkPMColor gSurfaceStorage[gSurfaceSize * gSurfaceSize];
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 }
31 41
32 switch (surfaceType) { 42 switch (surfaceType) {
33 case kRaster_SurfaceType: 43 case kRaster_SurfaceType:
34 return SkSurface::NewRaster(info); 44 return SkSurface::NewRaster(info);
35 case kGpu_SurfaceType: 45 case kRasterDirect_SurfaceType:
46 return SkSurface::NewRasterDirect(info, gSurfaceStorage,
47 info.minRowBytes());
48 case kGpu_SurfaceType:
36 #if SK_SUPPORT_GPU 49 #if SK_SUPPORT_GPU
37 SkASSERT(NULL != context); 50 return context ? SkSurface::NewRenderTarget(context, info) : NULL;
38 return SkSurface::NewRenderTarget(context, info);
39 #else
40 SkASSERT(0);
41 #endif 51 #endif
42 case kPicture_SurfaceType: 52 break;
43 return SkSurface::NewPicture(info.fWidth, info.fHeight); 53 case kPicture_SurfaceType:
54 return SkSurface::NewPicture(info.fWidth, info.fHeight);
44 } 55 }
45 SkASSERT(0);
46 return NULL; 56 return NULL;
47 } 57 }
48 58
49 enum ImageType { 59 enum ImageType {
50 kRasterCopy_ImageType, 60 kRasterCopy_ImageType,
51 kRasterData_ImageType, 61 kRasterData_ImageType,
52 kGpu_ImageType, 62 kGpu_ImageType,
53 kPicture_ImageType, 63 kPicture_ImageType,
54 kCodec_ImageType, 64 kCodec_ImageType,
55 }; 65 };
(...skipping 54 matching lines...) Expand 10 before | Expand all | Expand 10 after
110 static const struct { 120 static const struct {
111 ImageType fType; 121 ImageType fType;
112 bool fPeekShouldSucceed; 122 bool fPeekShouldSucceed;
113 } gRec[] = { 123 } gRec[] = {
114 { kRasterCopy_ImageType, true }, 124 { kRasterCopy_ImageType, true },
115 { kRasterData_ImageType, true }, 125 { kRasterData_ImageType, true },
116 { kGpu_ImageType, false }, 126 { kGpu_ImageType, false },
117 { kPicture_ImageType, false }, 127 { kPicture_ImageType, false },
118 { kCodec_ImageType, false }, 128 { kCodec_ImageType, false },
119 }; 129 };
120 130
121 const SkColor color = SK_ColorRED; 131 const SkColor color = SK_ColorRED;
122 const SkPMColor pmcolor = SkPreMultiplyColor(color); 132 const SkPMColor pmcolor = SkPreMultiplyColor(color);
123 133
124 for (size_t i = 0; i < SK_ARRAY_COUNT(gRec); ++i) { 134 for (size_t i = 0; i < SK_ARRAY_COUNT(gRec); ++i) {
125 SkImageInfo info; 135 SkImageInfo info;
126 size_t rowBytes; 136 size_t rowBytes;
127 137
128 SkAutoTUnref<SkImage> image(createImage(gRec[i].fType, NULL, color)); 138 SkAutoTUnref<SkImage> image(createImage(gRec[i].fType, NULL, color));
129 if (!image.get()) { 139 if (!image.get()) {
130 continue; // gpu may not be enabled 140 continue; // gpu may not be enabled
131 } 141 }
132 const void* addr = image->peekPixels(&info, &rowBytes); 142 const void* addr = image->peekPixels(&info, &rowBytes);
133 bool success = (NULL != addr); 143 bool success = (NULL != addr);
134 REPORTER_ASSERT(reporter, gRec[i].fPeekShouldSucceed == success); 144 REPORTER_ASSERT(reporter, gRec[i].fPeekShouldSucceed == success);
135 if (success) { 145 if (success) {
136 REPORTER_ASSERT(reporter, 10 == info.fWidth); 146 REPORTER_ASSERT(reporter, 10 == info.fWidth);
137 REPORTER_ASSERT(reporter, 10 == info.fHeight); 147 REPORTER_ASSERT(reporter, 10 == info.fHeight);
138 REPORTER_ASSERT(reporter, kPMColor_SkColorType == info.fColorType); 148 REPORTER_ASSERT(reporter, kPMColor_SkColorType == info.fColorType);
139 REPORTER_ASSERT(reporter, kPremul_SkAlphaType == info.fAlphaType || 149 REPORTER_ASSERT(reporter, kPremul_SkAlphaType == info.fAlphaType ||
140 kOpaque_SkAlphaType == info.fAlphaType); 150 kOpaque_SkAlphaType == info.fAlphaType);
141 REPORTER_ASSERT(reporter, info.minRowBytes() <= rowBytes); 151 REPORTER_ASSERT(reporter, info.minRowBytes() <= rowBytes);
142 REPORTER_ASSERT(reporter, pmcolor == *(const SkPMColor*)addr); 152 REPORTER_ASSERT(reporter, pmcolor == *(const SkPMColor*)addr);
143 } 153 }
144 } 154 }
145 } 155 }
146 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
147 static void TestSurfaceCopyOnWrite(skiatest::Reporter* reporter, SurfaceType sur faceType, 209 static void TestSurfaceCopyOnWrite(skiatest::Reporter* reporter, SurfaceType sur faceType,
148 GrContext* context) { 210 GrContext* context) {
149 // Verify that the right canvas commands trigger a copy on write 211 // Verify that the right canvas commands trigger a copy on write
150 SkSurface* surface = createSurface(surfaceType, context); 212 SkSurface* surface = createSurface(surfaceType, context);
151 SkAutoTUnref<SkSurface> aur_surface(surface); 213 SkAutoTUnref<SkSurface> aur_surface(surface);
152 SkCanvas* canvas = surface->getCanvas(); 214 SkCanvas* canvas = surface->getCanvas();
153 215
154 const SkRect testRect = 216 const SkRect testRect =
155 SkRect::MakeXYWH(SkIntToScalar(0), SkIntToScalar(0), 217 SkRect::MakeXYWH(SkIntToScalar(0), SkIntToScalar(0),
156 SkIntToScalar(4), SkIntToScalar(5)); 218 SkIntToScalar(4), SkIntToScalar(5));
(...skipping 171 matching lines...) Expand 10 before | Expand all | Expand 10 after
328 390
329 DEF_GPUTEST(Surface, reporter, factory) { 391 DEF_GPUTEST(Surface, reporter, factory) {
330 test_image(reporter); 392 test_image(reporter);
331 393
332 TestSurfaceCopyOnWrite(reporter, kRaster_SurfaceType, NULL); 394 TestSurfaceCopyOnWrite(reporter, kRaster_SurfaceType, NULL);
333 TestSurfaceCopyOnWrite(reporter, kPicture_SurfaceType, NULL); 395 TestSurfaceCopyOnWrite(reporter, kPicture_SurfaceType, NULL);
334 TestSurfaceWritableAfterSnapshotRelease(reporter, kRaster_SurfaceType, NULL) ; 396 TestSurfaceWritableAfterSnapshotRelease(reporter, kRaster_SurfaceType, NULL) ;
335 TestSurfaceWritableAfterSnapshotRelease(reporter, kPicture_SurfaceType, NULL ); 397 TestSurfaceWritableAfterSnapshotRelease(reporter, kPicture_SurfaceType, NULL );
336 TestSurfaceNoCanvas(reporter, kRaster_SurfaceType, NULL, SkSurface::kDiscard _ContentChangeMode); 398 TestSurfaceNoCanvas(reporter, kRaster_SurfaceType, NULL, SkSurface::kDiscard _ContentChangeMode);
337 TestSurfaceNoCanvas(reporter, kRaster_SurfaceType, NULL, SkSurface::kRetain_ ContentChangeMode); 399 TestSurfaceNoCanvas(reporter, kRaster_SurfaceType, NULL, SkSurface::kRetain_ ContentChangeMode);
400
338 test_imagepeek(reporter); 401 test_imagepeek(reporter);
402 test_canvaspeek(reporter, factory);
403
339 #if SK_SUPPORT_GPU 404 #if SK_SUPPORT_GPU
340 TestGetTexture(reporter, kRaster_SurfaceType, NULL); 405 TestGetTexture(reporter, kRaster_SurfaceType, NULL);
341 TestGetTexture(reporter, kPicture_SurfaceType, NULL); 406 TestGetTexture(reporter, kPicture_SurfaceType, NULL);
342 if (NULL != factory) { 407 if (NULL != factory) {
343 GrContext* context = factory->get(GrContextFactory::kNative_GLContextTyp e); 408 GrContext* context = factory->get(GrContextFactory::kNative_GLContextTyp e);
344 if (NULL != context) { 409 if (NULL != context) {
345 Test_crbug263329(reporter, context); 410 Test_crbug263329(reporter, context);
346 TestSurfaceCopyOnWrite(reporter, kGpu_SurfaceType, context); 411 TestSurfaceCopyOnWrite(reporter, kGpu_SurfaceType, context);
347 TestSurfaceWritableAfterSnapshotRelease(reporter, kGpu_SurfaceType, context); 412 TestSurfaceWritableAfterSnapshotRelease(reporter, kGpu_SurfaceType, context);
348 TestSurfaceNoCanvas(reporter, kGpu_SurfaceType, context, SkSurface:: kDiscard_ContentChangeMode); 413 TestSurfaceNoCanvas(reporter, kGpu_SurfaceType, context, SkSurface:: kDiscard_ContentChangeMode);
349 TestSurfaceNoCanvas(reporter, kGpu_SurfaceType, context, SkSurface:: kRetain_ContentChangeMode); 414 TestSurfaceNoCanvas(reporter, kGpu_SurfaceType, context, SkSurface:: kRetain_ContentChangeMode);
350 TestGetTexture(reporter, kGpu_SurfaceType, context); 415 TestGetTexture(reporter, kGpu_SurfaceType, context);
351 } 416 }
352 } 417 }
353 #endif 418 #endif
354 } 419 }
OLDNEW
« no previous file with comments | « tests/SkpSkGrTest.cpp ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698