• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright 2014 Google Inc.
3  *
4  * Use of this source code is governed by a BSD-style license that can be
5  * found in the LICENSE file.
6  */
7 
8 #include "SkCanvas.h"
9 #include "SkImage.h"
10 #include "SkShader.h"
11 #include "SkSurface.h"
12 #include "SkTypes.h"
13 #include "Test.h"
14 
15 #if SK_SUPPORT_GPU
16 #include "GrContext.h"
17 #endif
18 
test_bitmap_equality(skiatest::Reporter * reporter,SkBitmap & bm1,SkBitmap & bm2)19 static void test_bitmap_equality(skiatest::Reporter* reporter, SkBitmap& bm1, SkBitmap& bm2) {
20     SkAutoLockPixels lockBm1(bm1);
21     SkAutoLockPixels lockBm2(bm2);
22 
23     REPORTER_ASSERT(reporter, bm1.getSize() == bm2.getSize());
24     REPORTER_ASSERT(reporter, 0 == memcmp(bm1.getPixels(), bm2.getPixels(), bm1.getSize()));
25 }
26 
paint_source(SkSurface * sourceSurface)27 static void paint_source(SkSurface* sourceSurface) {
28     SkCanvas* sourceCanvas = sourceSurface->getCanvas();
29     sourceCanvas->clear(0xFFDEDEDE);
30 
31     SkPaint paintColor;
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 
run_shader_test(skiatest::Reporter * reporter,SkSurface * sourceSurface,SkSurface * destinationSurface,SkImageInfo & info)44 static void run_shader_test(skiatest::Reporter* reporter, SkSurface* sourceSurface,
45                             SkSurface* destinationSurface, SkImageInfo& info) {
46     paint_source(sourceSurface);
47 
48     sk_sp<SkImage> sourceImage(sourceSurface->makeImageSnapshot());
49     sk_sp<SkShader> sourceShader = sourceImage->makeShader(
50             SkShader::kRepeat_TileMode,
51             SkShader::kRepeat_TileMode);
52 
53     SkPaint paint;
54     paint.setShader(sourceShader);
55 
56     SkCanvas* destinationCanvas = destinationSurface->getCanvas();
57     destinationCanvas->clear(SK_ColorTRANSPARENT);
58     destinationCanvas->drawPaint(paint);
59 
60     SkIRect rect = info.bounds();
61 
62     SkBitmap bmOrig;
63     sourceSurface->getCanvas()->readPixels(rect, &bmOrig);
64 
65 
66     SkBitmap bm;
67     destinationCanvas->readPixels(rect, &bm);
68 
69     test_bitmap_equality(reporter, bmOrig, bm);
70 
71     // Test with a translated shader
72     SkMatrix matrix;
73     matrix.setTranslate(SkIntToScalar(-1), SkIntToScalar(0));
74 
75     sk_sp<SkShader> sourceShaderTranslated = sourceImage->makeShader(
76             SkShader::kRepeat_TileMode,
77             SkShader::kRepeat_TileMode,
78             &matrix);
79 
80     destinationCanvas->clear(SK_ColorTRANSPARENT);
81 
82     SkPaint paintTranslated;
83     paintTranslated.setShader(sourceShaderTranslated);
84 
85     destinationCanvas->drawPaint(paintTranslated);
86 
87     SkBitmap bmt;
88     destinationCanvas->readPixels(rect, &bmt);
89 
90     //  Test correctness
91     {
92         SkAutoLockPixels lockBm(bmt);
93         for (int y = 0; y < info.height(); y++) {
94             REPORTER_ASSERT(reporter, 0xFFFF0000 == bmt.getColor(0, y));
95 
96             for (int x = 1; x < info.width(); x++) {
97                 REPORTER_ASSERT(reporter, 0xFFDEDEDE == bmt.getColor(x, y));
98             }
99         }
100     }
101 }
102 
DEF_TEST(ImageNewShader,reporter)103 DEF_TEST(ImageNewShader, reporter) {
104     SkImageInfo info = SkImageInfo::MakeN32Premul(5, 5);
105 
106     auto sourceSurface(SkSurface::MakeRaster(info));
107     auto destinationSurface(SkSurface::MakeRaster(info));
108 
109     run_shader_test(reporter, sourceSurface.get(), destinationSurface.get(), info);
110 }
111 
112 #if SK_SUPPORT_GPU
113 
gpu_to_gpu(skiatest::Reporter * reporter,GrContext * context)114 static void gpu_to_gpu(skiatest::Reporter* reporter, GrContext* context) {
115     SkImageInfo info = SkImageInfo::MakeN32Premul(5, 5);
116 
117     auto sourceSurface(SkSurface::MakeRenderTarget(context, SkBudgeted::kNo, info));
118     auto destinationSurface(SkSurface::MakeRenderTarget(context, SkBudgeted::kNo, info));
119 
120     run_shader_test(reporter, sourceSurface.get(), destinationSurface.get(), info);
121 }
122 
gpu_to_raster(skiatest::Reporter * reporter,GrContext * context)123 static void gpu_to_raster(skiatest::Reporter* reporter, GrContext* context) {
124     SkImageInfo info = SkImageInfo::MakeN32Premul(5, 5);
125 
126     auto sourceSurface(SkSurface::MakeRenderTarget(context, SkBudgeted::kNo, info));
127     auto destinationSurface(SkSurface::MakeRaster(info));
128 
129     run_shader_test(reporter, sourceSurface.get(), destinationSurface.get(), info);
130 }
131 
raster_to_gpu(skiatest::Reporter * reporter,GrContext * context)132 static void raster_to_gpu(skiatest::Reporter* reporter, GrContext* context) {
133     SkImageInfo info = SkImageInfo::MakeN32Premul(5, 5);
134 
135     auto sourceSurface(SkSurface::MakeRaster(info));
136     auto destinationSurface(SkSurface::MakeRenderTarget(context, SkBudgeted::kNo, info));
137 
138     run_shader_test(reporter, sourceSurface.get(), destinationSurface.get(), info);
139 }
140 
DEF_GPUTEST_FOR_RENDERING_CONTEXTS(ImageNewShader_GPU,reporter,ctxInfo)141 DEF_GPUTEST_FOR_RENDERING_CONTEXTS(ImageNewShader_GPU, reporter, ctxInfo) {
142     //  GPU -> GPU
143     gpu_to_gpu(reporter, ctxInfo.grContext());
144 
145     //  GPU -> RASTER
146     gpu_to_raster(reporter, ctxInfo.grContext());
147 
148     //  RASTER -> GPU
149     raster_to_gpu(reporter, ctxInfo.grContext());
150 }
151 
152 #endif
153