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

Side by Side Diff: tests/SurfaceTest.cpp

Issue 155763004: add ways to peer into an image to get its pixels (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 | « src/image/SkImage_Raster.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"
10 #include "SkImageEncoder.h"
9 #include "SkRRect.h" 11 #include "SkRRect.h"
10 #include "SkSurface.h" 12 #include "SkSurface.h"
13 #include "SkUtils.h"
11 #include "Test.h" 14 #include "Test.h"
12 15
13 #if SK_SUPPORT_GPU 16 #if SK_SUPPORT_GPU
14 #include "GrContextFactory.h" 17 #include "GrContextFactory.h"
15 #else 18 #else
16 class GrContextFactory; 19 class GrContextFactory;
17 class GrContext; 20 class GrContext;
18 #endif 21 #endif
19 22
20 enum SurfaceType { 23 enum SurfaceType {
21 kRaster_SurfaceType, 24 kRaster_SurfaceType,
22 kGpu_SurfaceType, 25 kGpu_SurfaceType,
23 kPicture_SurfaceType 26 kPicture_SurfaceType
24 }; 27 };
25 28
26 static SkSurface* createSurface(SurfaceType surfaceType, GrContext* context) { 29 static SkSurface* createSurface(SurfaceType surfaceType, GrContext* context) {
27 static const SkImageInfo imageSpec = { 30 static const SkImageInfo info = SkImageInfo::MakeN32Premul(10, 10);
28 10, // width
29 10, // height
30 kPMColor_SkColorType,
31 kPremul_SkAlphaType
32 };
33 31
34 switch (surfaceType) { 32 switch (surfaceType) {
35 case kRaster_SurfaceType: 33 case kRaster_SurfaceType:
36 return SkSurface::NewRaster(imageSpec); 34 return SkSurface::NewRaster(info);
37 case kGpu_SurfaceType: 35 case kGpu_SurfaceType:
38 #if SK_SUPPORT_GPU 36 #if SK_SUPPORT_GPU
39 SkASSERT(NULL != context); 37 SkASSERT(NULL != context);
40 return SkSurface::NewRenderTarget(context, imageSpec); 38 return SkSurface::NewRenderTarget(context, info);
41 #else 39 #else
42 SkASSERT(0); 40 SkASSERT(0);
43 #endif 41 #endif
44 case kPicture_SurfaceType: 42 case kPicture_SurfaceType:
45 return SkSurface::NewPicture(10, 10); 43 return SkSurface::NewPicture(info.fWidth, info.fHeight);
46 } 44 }
47 SkASSERT(0); 45 SkASSERT(0);
48 return NULL; 46 return NULL;
49 } 47 }
50 48
51 #include "SkData.h" 49 enum ImageType {
50 kRasterCopy_ImageType,
51 kRasterData_ImageType,
52 kGpu_ImageType,
53 kPicture_ImageType,
54 kCodec_ImageType,
55 };
52 56
53 static void test_image(skiatest::Reporter* reporter) { 57 static void test_image(skiatest::Reporter* reporter) {
54 SkImageInfo info = SkImageInfo::MakeN32Premul(1, 1); 58 SkImageInfo info = SkImageInfo::MakeN32Premul(1, 1);
55 size_t rowBytes = info.minRowBytes(); 59 size_t rowBytes = info.minRowBytes();
56 size_t size = info.getSafeSize(rowBytes); 60 size_t size = info.getSafeSize(rowBytes);
57 void* addr = sk_malloc_throw(size); 61 void* addr = sk_malloc_throw(size);
58 SkData* data = SkData::NewFromMalloc(addr, size); 62 SkData* data = SkData::NewFromMalloc(addr, size);
59 63
60 REPORTER_ASSERT(reporter, 1 == data->getRefCnt()); 64 REPORTER_ASSERT(reporter, 1 == data->getRefCnt());
61 SkImage* image = SkImage::NewRasterData(info, data, rowBytes); 65 SkImage* image = SkImage::NewRasterData(info, data, rowBytes);
62 REPORTER_ASSERT(reporter, 2 == data->getRefCnt()); 66 REPORTER_ASSERT(reporter, 2 == data->getRefCnt());
63 image->unref(); 67 image->unref();
64 REPORTER_ASSERT(reporter, 1 == data->getRefCnt()); 68 REPORTER_ASSERT(reporter, 1 == data->getRefCnt());
65 data->unref(); 69 data->unref();
66 } 70 }
67 71
72 static SkImage* createImage(ImageType imageType, GrContext* context,
73 SkColor color) {
74 const SkPMColor pmcolor = SkPreMultiplyColor(color);
75 const SkImageInfo info = SkImageInfo::MakeN32Premul(10, 10);
76 const size_t rowBytes = info.minRowBytes();
77 const size_t size = rowBytes * info.fHeight;
78
79 void* addr = sk_malloc_throw(size);
80 sk_memset32((SkPMColor*)addr, pmcolor, SkToInt(size >> 2));
81 SkAutoTUnref<SkData> data(SkData::NewFromMalloc(addr, size));
82
83 switch (imageType) {
84 case kRasterCopy_ImageType:
85 return SkImage::NewRasterCopy(info, addr, rowBytes);
86 case kRasterData_ImageType:
87 return SkImage::NewRasterData(info, data, rowBytes);
88 case kGpu_ImageType:
89 return NULL; // TODO
90 case kPicture_ImageType: {
91 SkAutoTUnref<SkSurface> surf(SkSurface::NewPicture(info.fWidth,
92 info.fHeight));
93 surf->getCanvas()->drawColor(SK_ColorRED);
94 return surf->newImageSnapshot();
95 }
96 case kCodec_ImageType: {
97 SkBitmap bitmap;
98 bitmap.installPixels(info, addr, rowBytes, NULL, NULL);
99 SkAutoTUnref<SkData> src(
100 SkImageEncoder::EncodeData(bitmap, SkImageEncoder::kPNG_Type,
101 100));
102 return SkImage::NewEncodedData(src);
103 }
104 }
105 SkASSERT(false);
106 return NULL;
107 }
108
109 static void test_imagepeek(skiatest::Reporter* reporter) {
110 static const struct {
111 ImageType fType;
112 bool fPeekShouldSucceed;
113 } gRec[] = {
114 { kRasterCopy_ImageType, true },
115 { kRasterData_ImageType, true },
116 { kGpu_ImageType, false },
117 { kPicture_ImageType, false },
118 { kCodec_ImageType, false },
119 };
120
121 const SkColor color = SK_ColorRED;
122 const SkPMColor pmcolor = SkPreMultiplyColor(color);
123
124 for (size_t i = 0; i < SK_ARRAY_COUNT(gRec); ++i) {
125 SkImageInfo info;
126 size_t rowBytes;
127
128 SkAutoTUnref<SkImage> image(createImage(gRec[i].fType, NULL, color));
129 if (!image.get()) {
130 continue; // gpu may not be enabled
131 }
132 const void* addr = image->peekPixels(&info, &rowBytes);
133 bool success = (NULL != addr);
134 REPORTER_ASSERT(reporter, gRec[i].fPeekShouldSucceed == success);
135 if (success) {
136 REPORTER_ASSERT(reporter, 10 == info.fWidth);
137 REPORTER_ASSERT(reporter, 10 == info.fHeight);
138 REPORTER_ASSERT(reporter, kPMColor_SkColorType == info.fColorType);
139 REPORTER_ASSERT(reporter, kPremul_SkAlphaType == info.fAlphaType ||
140 kOpaque_SkAlphaType == info.fAlphaType);
141 REPORTER_ASSERT(reporter, info.minRowBytes() <= rowBytes);
142 REPORTER_ASSERT(reporter, pmcolor == *(const SkPMColor*)addr);
143 }
144 }
145 }
146
68 static void TestSurfaceCopyOnWrite(skiatest::Reporter* reporter, SurfaceType sur faceType, 147 static void TestSurfaceCopyOnWrite(skiatest::Reporter* reporter, SurfaceType sur faceType,
69 GrContext* context) { 148 GrContext* context) {
70 // Verify that the right canvas commands trigger a copy on write 149 // Verify that the right canvas commands trigger a copy on write
71 SkSurface* surface = createSurface(surfaceType, context); 150 SkSurface* surface = createSurface(surfaceType, context);
72 SkAutoTUnref<SkSurface> aur_surface(surface); 151 SkAutoTUnref<SkSurface> aur_surface(surface);
73 SkCanvas* canvas = surface->getCanvas(); 152 SkCanvas* canvas = surface->getCanvas();
74 153
75 const SkRect testRect = 154 const SkRect testRect =
76 SkRect::MakeXYWH(SkIntToScalar(0), SkIntToScalar(0), 155 SkRect::MakeXYWH(SkIntToScalar(0), SkIntToScalar(0),
77 SkIntToScalar(4), SkIntToScalar(5)); 156 SkIntToScalar(4), SkIntToScalar(5));
(...skipping 172 matching lines...) Expand 10 before | Expand all | Expand 10 after
250 329
251 DEF_GPUTEST(Surface, reporter, factory) { 330 DEF_GPUTEST(Surface, reporter, factory) {
252 test_image(reporter); 331 test_image(reporter);
253 332
254 TestSurfaceCopyOnWrite(reporter, kRaster_SurfaceType, NULL); 333 TestSurfaceCopyOnWrite(reporter, kRaster_SurfaceType, NULL);
255 TestSurfaceCopyOnWrite(reporter, kPicture_SurfaceType, NULL); 334 TestSurfaceCopyOnWrite(reporter, kPicture_SurfaceType, NULL);
256 TestSurfaceWritableAfterSnapshotRelease(reporter, kRaster_SurfaceType, NULL) ; 335 TestSurfaceWritableAfterSnapshotRelease(reporter, kRaster_SurfaceType, NULL) ;
257 TestSurfaceWritableAfterSnapshotRelease(reporter, kPicture_SurfaceType, NULL ); 336 TestSurfaceWritableAfterSnapshotRelease(reporter, kPicture_SurfaceType, NULL );
258 TestSurfaceNoCanvas(reporter, kRaster_SurfaceType, NULL, SkSurface::kDiscard _ContentChangeMode); 337 TestSurfaceNoCanvas(reporter, kRaster_SurfaceType, NULL, SkSurface::kDiscard _ContentChangeMode);
259 TestSurfaceNoCanvas(reporter, kRaster_SurfaceType, NULL, SkSurface::kRetain_ ContentChangeMode); 338 TestSurfaceNoCanvas(reporter, kRaster_SurfaceType, NULL, SkSurface::kRetain_ ContentChangeMode);
339 test_imagepeek(reporter);
260 #if SK_SUPPORT_GPU 340 #if SK_SUPPORT_GPU
261 TestGetTexture(reporter, kRaster_SurfaceType, NULL); 341 TestGetTexture(reporter, kRaster_SurfaceType, NULL);
262 TestGetTexture(reporter, kPicture_SurfaceType, NULL); 342 TestGetTexture(reporter, kPicture_SurfaceType, NULL);
263 if (NULL != factory) { 343 if (NULL != factory) {
264 GrContext* context = factory->get(GrContextFactory::kNative_GLContextTyp e); 344 GrContext* context = factory->get(GrContextFactory::kNative_GLContextTyp e);
265 if (NULL != context) { 345 if (NULL != context) {
266 Test_crbug263329(reporter, context); 346 Test_crbug263329(reporter, context);
267 TestSurfaceCopyOnWrite(reporter, kGpu_SurfaceType, context); 347 TestSurfaceCopyOnWrite(reporter, kGpu_SurfaceType, context);
268 TestSurfaceWritableAfterSnapshotRelease(reporter, kGpu_SurfaceType, context); 348 TestSurfaceWritableAfterSnapshotRelease(reporter, kGpu_SurfaceType, context);
269 TestSurfaceNoCanvas(reporter, kGpu_SurfaceType, context, SkSurface:: kDiscard_ContentChangeMode); 349 TestSurfaceNoCanvas(reporter, kGpu_SurfaceType, context, SkSurface:: kDiscard_ContentChangeMode);
270 TestSurfaceNoCanvas(reporter, kGpu_SurfaceType, context, SkSurface:: kRetain_ContentChangeMode); 350 TestSurfaceNoCanvas(reporter, kGpu_SurfaceType, context, SkSurface:: kRetain_ContentChangeMode);
271 TestGetTexture(reporter, kGpu_SurfaceType, context); 351 TestGetTexture(reporter, kGpu_SurfaceType, context);
272 } 352 }
273 } 353 }
274 #endif 354 #endif
275 } 355 }
OLDNEW
« no previous file with comments | « src/image/SkImage_Raster.cpp ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698