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

Side by Side Diff: tests/ImageNewShaderTest.cpp

Issue 409653003: Now able to set the localMatrix when creating a SkShader from a SkImage (Closed) Base URL: https://skia.googlesource.com/skia.git@master
Patch Set: Corrects warnings in windows build Created 6 years, 5 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
« 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 2014 Google Inc. 2 * Copyright 2014 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 "SkTypes.h" 8 #include "SkTypes.h"
9 #if SK_SUPPORT_GPU 9 #if SK_SUPPORT_GPU
10 #include "GrContextFactory.h" 10 #include "GrContextFactory.h"
11 #endif 11 #endif
12 #include "SkCanvas.h" 12 #include "SkCanvas.h"
13 #include "SkImage.h" 13 #include "SkImage.h"
14 #include "SkShader.h" 14 #include "SkShader.h"
15 #include "SkSurface.h" 15 #include "SkSurface.h"
16 16
17 #include "Test.h" 17 #include "Test.h"
18 18
19 void testBitmapEquality(skiatest::Reporter* reporter, SkBitmap& bm1, SkBitmap& b m2) { 19 void testBitmapEquality(skiatest::Reporter* reporter, SkBitmap& bm1, SkBitmap& b m2) {
20 bm1.lockPixels(); 20 SkAutoLockPixels lockBm1(bm1);
21 bm2.lockPixels(); 21 SkAutoLockPixels lockBm2(bm2);
22 22
23 REPORTER_ASSERT(reporter, bm1.getSize() == bm2.getSize()); 23 REPORTER_ASSERT(reporter, bm1.getSize() == bm2.getSize());
24 REPORTER_ASSERT(reporter, 0 == memcmp(bm1.getPixels(), bm2.getPixels(), bm1. getSize())); 24 REPORTER_ASSERT(reporter, 0 == memcmp(bm1.getPixels(), bm2.getPixels(), bm1. getSize()));
25
26 bm2.unlockPixels();
27 bm1.unlockPixels();
28 } 25 }
29 26
30 void runShaderTest(skiatest::Reporter* reporter, SkSurface* source, SkSurface* d estination, SkImageInfo& info) { 27 void paintSource(SkSurface* sourceSurface) {
31 SkCanvas* rasterCanvas = source->getCanvas(); 28 SkCanvas* sourceCanvas = sourceSurface->getCanvas();
32 rasterCanvas->drawColor(0xFFDEDEDE, SkXfermode::kSrc_Mode); 29 sourceCanvas->clear(0xFFDEDEDE);
33 30
34 SkAutoTUnref<SkImage> rasterImage(source->newImageSnapshot()); 31 SkPaint paintColor;
35 SkAutoTUnref<SkShader> rasterShader(rasterImage->newShader( 32 paintColor.setColor(0xFFFF0000);
33 paintColor.setStyle(SkPaint::kFill_Style);
34
35 SkRect rect = SkRect::MakeXYWH(
36 SkIntToScalar(1),
37 SkIntToScalar(0),
38 SkIntToScalar(1),
39 SkIntToScalar(sourceSurface->height()));
40
41 sourceCanvas->drawRect(rect, paintColor);
42 }
43
44 void runShaderTest(skiatest::Reporter* reporter, SkSurface* sourceSurface, SkSur face* destinationSurface, SkImageInfo& info) {
45 paintSource(sourceSurface);
46
47 SkAutoTUnref<SkImage> sourceImage(sourceSurface->newImageSnapshot());
48 SkAutoTUnref<SkShader> sourceShader(sourceImage->newShader(
36 SkShader::kRepeat_TileMode, 49 SkShader::kRepeat_TileMode,
37 SkShader::kRepeat_TileMode)); 50 SkShader::kRepeat_TileMode));
38 51
39 SkPaint paint; 52 SkPaint paint;
40 paint.setShader(rasterShader); 53 paint.setShader(sourceShader);
41 SkCanvas* canvasDest = destination->getCanvas();
42 canvasDest->clear(SK_ColorTRANSPARENT);
43 canvasDest->drawPaint(paint);
44 54
45 SkIRect rect = SkIRect::MakeXYWH(0, 0, 5, 5); 55 SkCanvas* destinationCanvas = destinationSurface->getCanvas();
56 destinationCanvas->clear(SK_ColorTRANSPARENT);
57 destinationCanvas->drawPaint(paint);
58
59 SkIRect rect = SkIRect::MakeWH(info.width(), info.height());
46 60
47 SkBitmap bmOrig; 61 SkBitmap bmOrig;
48 rasterCanvas->readPixels(rect, &bmOrig); 62 sourceSurface->getCanvas()->readPixels(rect, &bmOrig);
63
49 64
50 SkBitmap bm; 65 SkBitmap bm;
51 canvasDest->readPixels(rect, &bm); 66 destinationCanvas->readPixels(rect, &bm);
52 67
53 testBitmapEquality(reporter, bmOrig, bm); 68 testBitmapEquality(reporter, bmOrig, bm);
69
70
71
72 // Test with a translated shader
73 SkMatrix matrix;
74 matrix.setTranslate(SkIntToScalar(-1), SkIntToScalar(0));
75
76 SkAutoTUnref<SkShader> sourceShaderTranslated(sourceImage->newShader(
77 SkShader::kRepeat_TileMode,
78 SkShader::kRepeat_TileMode,
79 &matrix));
80
81 destinationCanvas->clear(SK_ColorTRANSPARENT);
82
83 SkPaint paintTranslated;
84 paintTranslated.setShader(sourceShaderTranslated);
85
86 destinationCanvas->drawPaint(paintTranslated);
87
88 SkBitmap bmt;
89 destinationCanvas->readPixels(rect, &bmt);
90
91 // Test correctness
92 {
93 SkAutoLockPixels lockBm(bmt);
94 for (int y = 0; y < info.height(); y++) {
95 REPORTER_ASSERT(reporter, 0xFFFF0000 == bmt.getColor(0, y));
96
97 for (int x = 1; x < info.width(); x++) {
98 REPORTER_ASSERT(reporter, 0xFFDEDEDE == bmt.getColor(x, y));
99 }
100 }
101 }
54 } 102 }
55 103
56 DEF_TEST(ImageNewShader, reporter) { 104 DEF_TEST(ImageNewShader, reporter) {
57 SkImageInfo info = SkImageInfo::MakeN32Premul(5, 5); 105 SkImageInfo info = SkImageInfo::MakeN32Premul(5, 5);
58 106
59 SkAutoTUnref<SkSurface> srcSurface(SkSurface::NewRaster(info)); 107 SkAutoTUnref<SkSurface> sourceSurface(SkSurface::NewRaster(info));
60 SkAutoTUnref<SkSurface> dstSurface(SkSurface::NewRaster(info)); 108 SkAutoTUnref<SkSurface> destinationSurface(SkSurface::NewRaster(info));
61 109
62 runShaderTest(reporter, srcSurface.get(), dstSurface.get(), info); 110 runShaderTest(reporter, sourceSurface.get(), destinationSurface.get(), info) ;
63 } 111 }
64 112
65 #if SK_SUPPORT_GPU 113 #if SK_SUPPORT_GPU
66 114
67 void gpuToGpu(skiatest::Reporter* reporter, GrContext* context) { 115 void gpuToGpu(skiatest::Reporter* reporter, GrContext* context) {
68 SkImageInfo info = SkImageInfo::MakeN32Premul(5, 5); 116 SkImageInfo info = SkImageInfo::MakeN32Premul(5, 5);
69 117
70 SkAutoTUnref<SkSurface> srcSurface(SkSurface::NewRenderTarget(context, info) ); 118 SkAutoTUnref<SkSurface> sourceSurface(SkSurface::NewRenderTarget(context, in fo));
71 SkAutoTUnref<SkSurface> dstSurface(SkSurface::NewRenderTarget(context, info) ); 119 SkAutoTUnref<SkSurface> destinationSurface(SkSurface::NewRenderTarget(contex t, info));
72 120
73 runShaderTest(reporter, srcSurface.get(), dstSurface.get(), info); 121 runShaderTest(reporter, sourceSurface.get(), destinationSurface.get(), info) ;
74 } 122 }
75 123
76 void gpuToRaster(skiatest::Reporter* reporter, GrContext* context) { 124 void gpuToRaster(skiatest::Reporter* reporter, GrContext* context) {
77 SkImageInfo info = SkImageInfo::MakeN32Premul(5, 5); 125 SkImageInfo info = SkImageInfo::MakeN32Premul(5, 5);
78 126
79 SkAutoTUnref<SkSurface> srcSurface(SkSurface::NewRenderTarget(context, info) ); 127 SkAutoTUnref<SkSurface> sourceSurface(SkSurface::NewRenderTarget(context, in fo));
80 SkAutoTUnref<SkSurface> dstSurface(SkSurface::NewRaster(info)); 128 SkAutoTUnref<SkSurface> destinationSurface(SkSurface::NewRaster(info));
81 129
82 runShaderTest(reporter, srcSurface.get(), dstSurface.get(), info); 130 runShaderTest(reporter, sourceSurface.get(), destinationSurface.get(), info) ;
83 } 131 }
84 132
85 void rasterToGpu(skiatest::Reporter* reporter, GrContext* context) { 133 void rasterToGpu(skiatest::Reporter* reporter, GrContext* context) {
86 SkImageInfo info = SkImageInfo::MakeN32Premul(5, 5); 134 SkImageInfo info = SkImageInfo::MakeN32Premul(5, 5);
87 135
88 SkAutoTUnref<SkSurface> srcSurface(SkSurface::NewRaster(info)); 136 SkAutoTUnref<SkSurface> sourceSurface(SkSurface::NewRaster(info));
89 SkAutoTUnref<SkSurface> dstSurface(SkSurface::NewRenderTarget(context, info) ); 137 SkAutoTUnref<SkSurface> destinationSurface(SkSurface::NewRenderTarget(contex t, info));
90 138
91 runShaderTest(reporter, srcSurface.get(), dstSurface.get(), info); 139 runShaderTest(reporter, sourceSurface.get(), destinationSurface.get(), info) ;
92 } 140 }
93 141
94 DEF_GPUTEST(ImageNewShader_GPU, reporter, factory) { 142 DEF_GPUTEST(ImageNewShader_GPU, reporter, factory) {
95 for (int i= 0; i < GrContextFactory::kGLContextTypeCnt; ++i) { 143 for (int i = 0; i < GrContextFactory::kGLContextTypeCnt; ++i) {
96 GrContextFactory::GLContextType glCtxType = (GrContextFactory::GLContext Type) i; 144 GrContextFactory::GLContextType glCtxType = (GrContextFactory::GLContext Type) i;
97 145
98 if (!GrContextFactory::IsRenderingGLContext(glCtxType)) { 146 if (!GrContextFactory::IsRenderingGLContext(glCtxType)) {
99 continue; 147 continue;
100 } 148 }
101 149
102 GrContext* context = factory->get(glCtxType); 150 GrContext* context = factory->get(glCtxType);
103 151
104 if (NULL == context) { 152 if (NULL == context) {
105 continue; 153 continue;
106 } 154 }
107 155
108 // GPU -> GPU 156 // GPU -> GPU
109 gpuToGpu(reporter, context); 157 gpuToGpu(reporter, context);
110 158
111 // GPU -> RASTER 159 // GPU -> RASTER
112 gpuToRaster(reporter, context); 160 gpuToRaster(reporter, context);
113 161
114 162 // RASTER -> GPU
115 // RASTER -> GPU
116 rasterToGpu(reporter, context); 163 rasterToGpu(reporter, context);
117 } 164 }
118 } 165 }
119 166
120 #endif 167 #endif
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