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

Side by Side Diff: tests/SurfaceTest.cpp

Issue 159723006: add peekPixels to canvas (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
« src/core/SkCanvas.cpp ('K') | « 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 static const struct {
159 SurfaceType fType;
160 bool fPeekShouldSucceed;
161 } gRec[] = {
162 { kRaster_SurfaceType, true },
163 { kRasterDirect_SurfaceType, true },
164 { kGpu_SurfaceType, false },
165 { kPicture_SurfaceType, false },
166 };
167
168 const SkColor color = SK_ColorRED;
169 const SkPMColor pmcolor = SkPreMultiplyColor(color);
170
171 for (size_t i = 0; i < SK_ARRAY_COUNT(gRec); ++i) {
172 SkImageInfo info, requestInfo;
173 size_t rowBytes;
174
175 SkAutoTUnref<SkSurface> surface(createSurface(gRec[i].fType, NULL,
176 &requestInfo));
177 if (!surface.get()) {
178 continue; // gpu may not be enabled
scroggo 2014/02/12 18:40:58 This could hide errors if one of the others fails
reed2 2014/02/13 14:43:35 Done.
179 }
180 surface->getCanvas()->clear(color);
181
182 const void* addr = surface->getCanvas()->peekPixels(&info, &rowBytes);
183 bool success = (NULL != addr);
184 REPORTER_ASSERT(reporter, gRec[i].fPeekShouldSucceed == success);
185
186 SkImageInfo info2;
187 size_t rb2;
188 const void* addr2 = surface->peekPixels(&info2, &rb2);
189
190 if (success) {
191 REPORTER_ASSERT(reporter, requestInfo == info);
192 REPORTER_ASSERT(reporter, requestInfo.minRowBytes() <= rowBytes);
193 REPORTER_ASSERT(reporter, pmcolor == *(const SkPMColor*)addr);
194
195 REPORTER_ASSERT(reporter, addr2 == addr);
196 REPORTER_ASSERT(reporter, info2 == info);
197 REPORTER_ASSERT(reporter, rb2 == rowBytes);
198 } else {
199 REPORTER_ASSERT(reporter, NULL == addr2);
200 }
201 }
202 }
203
147 static void TestSurfaceCopyOnWrite(skiatest::Reporter* reporter, SurfaceType sur faceType, 204 static void TestSurfaceCopyOnWrite(skiatest::Reporter* reporter, SurfaceType sur faceType,
148 GrContext* context) { 205 GrContext* context) {
149 // Verify that the right canvas commands trigger a copy on write 206 // Verify that the right canvas commands trigger a copy on write
150 SkSurface* surface = createSurface(surfaceType, context); 207 SkSurface* surface = createSurface(surfaceType, context);
151 SkAutoTUnref<SkSurface> aur_surface(surface); 208 SkAutoTUnref<SkSurface> aur_surface(surface);
152 SkCanvas* canvas = surface->getCanvas(); 209 SkCanvas* canvas = surface->getCanvas();
153 210
154 const SkRect testRect = 211 const SkRect testRect =
155 SkRect::MakeXYWH(SkIntToScalar(0), SkIntToScalar(0), 212 SkRect::MakeXYWH(SkIntToScalar(0), SkIntToScalar(0),
156 SkIntToScalar(4), SkIntToScalar(5)); 213 SkIntToScalar(4), SkIntToScalar(5));
(...skipping 172 matching lines...) Expand 10 before | Expand all | Expand 10 after
329 386
330 DEF_GPUTEST(Surface, reporter, factory) { 387 DEF_GPUTEST(Surface, reporter, factory) {
331 test_image(reporter); 388 test_image(reporter);
332 389
333 TestSurfaceCopyOnWrite(reporter, kRaster_SurfaceType, NULL); 390 TestSurfaceCopyOnWrite(reporter, kRaster_SurfaceType, NULL);
334 TestSurfaceCopyOnWrite(reporter, kPicture_SurfaceType, NULL); 391 TestSurfaceCopyOnWrite(reporter, kPicture_SurfaceType, NULL);
335 TestSurfaceWritableAfterSnapshotRelease(reporter, kRaster_SurfaceType, NULL) ; 392 TestSurfaceWritableAfterSnapshotRelease(reporter, kRaster_SurfaceType, NULL) ;
336 TestSurfaceWritableAfterSnapshotRelease(reporter, kPicture_SurfaceType, NULL ); 393 TestSurfaceWritableAfterSnapshotRelease(reporter, kPicture_SurfaceType, NULL );
337 TestSurfaceNoCanvas(reporter, kRaster_SurfaceType, NULL, SkSurface::kDiscard _ContentChangeMode); 394 TestSurfaceNoCanvas(reporter, kRaster_SurfaceType, NULL, SkSurface::kDiscard _ContentChangeMode);
338 TestSurfaceNoCanvas(reporter, kRaster_SurfaceType, NULL, SkSurface::kRetain_ ContentChangeMode); 395 TestSurfaceNoCanvas(reporter, kRaster_SurfaceType, NULL, SkSurface::kRetain_ ContentChangeMode);
396
339 test_imagepeek(reporter); 397 test_imagepeek(reporter);
398 test_canvaspeek(reporter);
399
340 #if SK_SUPPORT_GPU 400 #if SK_SUPPORT_GPU
341 TestGetTexture(reporter, kRaster_SurfaceType, NULL); 401 TestGetTexture(reporter, kRaster_SurfaceType, NULL);
342 TestGetTexture(reporter, kPicture_SurfaceType, NULL); 402 TestGetTexture(reporter, kPicture_SurfaceType, NULL);
343 if (NULL != factory) { 403 if (NULL != factory) {
344 GrContext* context = factory->get(GrContextFactory::kNative_GLContextTyp e); 404 GrContext* context = factory->get(GrContextFactory::kNative_GLContextTyp e);
345 if (NULL != context) { 405 if (NULL != context) {
346 Test_crbug263329(reporter, context); 406 Test_crbug263329(reporter, context);
347 TestSurfaceCopyOnWrite(reporter, kGpu_SurfaceType, context); 407 TestSurfaceCopyOnWrite(reporter, kGpu_SurfaceType, context);
348 TestSurfaceWritableAfterSnapshotRelease(reporter, kGpu_SurfaceType, context); 408 TestSurfaceWritableAfterSnapshotRelease(reporter, kGpu_SurfaceType, context);
349 TestSurfaceNoCanvas(reporter, kGpu_SurfaceType, context, SkSurface:: kDiscard_ContentChangeMode); 409 TestSurfaceNoCanvas(reporter, kGpu_SurfaceType, context, SkSurface:: kDiscard_ContentChangeMode);
350 TestSurfaceNoCanvas(reporter, kGpu_SurfaceType, context, SkSurface:: kRetain_ContentChangeMode); 410 TestSurfaceNoCanvas(reporter, kGpu_SurfaceType, context, SkSurface:: kRetain_ContentChangeMode);
351 TestGetTexture(reporter, kGpu_SurfaceType, context); 411 TestGetTexture(reporter, kGpu_SurfaceType, context);
352 } 412 }
353 } 413 }
354 #endif 414 #endif
355 } 415 }
OLDNEW
« src/core/SkCanvas.cpp ('K') | « tests/SkpSkGrTest.cpp ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698