blob: c78b7157a2bc457e4477203acfa8eaabc7ba1c98 [file] [log] [blame]
piotaixrcef04f82014-07-14 07:48:04 -07001/*
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
scroggo41b86872014-07-14 10:25:00 -07008#include "SkTypes.h"
piotaixrcef04f82014-07-14 07:48:04 -07009#if SK_SUPPORT_GPU
10#include "GrContextFactory.h"
11#endif
12#include "SkCanvas.h"
13#include "SkImage.h"
14#include "SkShader.h"
15#include "SkSurface.h"
16
17#include "Test.h"
18
19void testBitmapEquality(skiatest::Reporter* reporter, SkBitmap& bm1, SkBitmap& bm2) {
piotaixr76d5b472014-07-22 15:02:05 -070020 SkAutoLockPixels lockBm1(bm1);
21 SkAutoLockPixels lockBm2(bm2);
piotaixrcef04f82014-07-14 07:48:04 -070022
23 REPORTER_ASSERT(reporter, bm1.getSize() == bm2.getSize());
24 REPORTER_ASSERT(reporter, 0 == memcmp(bm1.getPixels(), bm2.getPixels(), bm1.getSize()));
piotaixrcef04f82014-07-14 07:48:04 -070025}
26
piotaixr76d5b472014-07-22 15:02:05 -070027void paintSource(SkSurface* sourceSurface) {
28 SkCanvas* sourceCanvas = sourceSurface->getCanvas();
29 sourceCanvas->clear(0xFFDEDEDE);
piotaixrcef04f82014-07-14 07:48:04 -070030
piotaixr76d5b472014-07-22 15:02:05 -070031 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
44void runShaderTest(skiatest::Reporter* reporter, SkSurface* sourceSurface, SkSurface* destinationSurface, SkImageInfo& info) {
45 paintSource(sourceSurface);
46
47 SkAutoTUnref<SkImage> sourceImage(sourceSurface->newImageSnapshot());
48 SkAutoTUnref<SkShader> sourceShader(sourceImage->newShader(
piotaixrcef04f82014-07-14 07:48:04 -070049 SkShader::kRepeat_TileMode,
50 SkShader::kRepeat_TileMode));
51
52 SkPaint paint;
piotaixr76d5b472014-07-22 15:02:05 -070053 paint.setShader(sourceShader);
piotaixrcef04f82014-07-14 07:48:04 -070054
piotaixr76d5b472014-07-22 15:02:05 -070055 SkCanvas* destinationCanvas = destinationSurface->getCanvas();
56 destinationCanvas->clear(SK_ColorTRANSPARENT);
57 destinationCanvas->drawPaint(paint);
58
halcanaryf622a6c2014-10-24 12:54:53 -070059 SkIRect rect = info.bounds();
piotaixrcef04f82014-07-14 07:48:04 -070060
61 SkBitmap bmOrig;
piotaixr76d5b472014-07-22 15:02:05 -070062 sourceSurface->getCanvas()->readPixels(rect, &bmOrig);
63
piotaixrcef04f82014-07-14 07:48:04 -070064
65 SkBitmap bm;
piotaixr76d5b472014-07-22 15:02:05 -070066 destinationCanvas->readPixels(rect, &bm);
piotaixrcef04f82014-07-14 07:48:04 -070067
68 testBitmapEquality(reporter, bmOrig, bm);
piotaixr76d5b472014-07-22 15:02:05 -070069
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 }
piotaixrcef04f82014-07-14 07:48:04 -0700102}
103
104DEF_TEST(ImageNewShader, reporter) {
105 SkImageInfo info = SkImageInfo::MakeN32Premul(5, 5);
106
piotaixr76d5b472014-07-22 15:02:05 -0700107 SkAutoTUnref<SkSurface> sourceSurface(SkSurface::NewRaster(info));
108 SkAutoTUnref<SkSurface> destinationSurface(SkSurface::NewRaster(info));
piotaixrcef04f82014-07-14 07:48:04 -0700109
piotaixr76d5b472014-07-22 15:02:05 -0700110 runShaderTest(reporter, sourceSurface.get(), destinationSurface.get(), info);
piotaixrcef04f82014-07-14 07:48:04 -0700111}
112
113#if SK_SUPPORT_GPU
114
115void gpuToGpu(skiatest::Reporter* reporter, GrContext* context) {
116 SkImageInfo info = SkImageInfo::MakeN32Premul(5, 5);
117
bsalomonafe30052015-01-16 07:32:33 -0800118 SkAutoTUnref<SkSurface> sourceSurface(
119 SkSurface::NewRenderTarget(context, SkSurface::kNo_Budgeted, info));
120 SkAutoTUnref<SkSurface> destinationSurface(
121 SkSurface::NewRenderTarget(context, SkSurface::kNo_Budgeted, info));
piotaixrcef04f82014-07-14 07:48:04 -0700122
piotaixr76d5b472014-07-22 15:02:05 -0700123 runShaderTest(reporter, sourceSurface.get(), destinationSurface.get(), info);
piotaixrcef04f82014-07-14 07:48:04 -0700124}
125
126void gpuToRaster(skiatest::Reporter* reporter, GrContext* context) {
127 SkImageInfo info = SkImageInfo::MakeN32Premul(5, 5);
128
bsalomonafe30052015-01-16 07:32:33 -0800129 SkAutoTUnref<SkSurface> sourceSurface(SkSurface::NewRenderTarget(context,
130 SkSurface::kNo_Budgeted, info));
piotaixr76d5b472014-07-22 15:02:05 -0700131 SkAutoTUnref<SkSurface> destinationSurface(SkSurface::NewRaster(info));
piotaixrcef04f82014-07-14 07:48:04 -0700132
piotaixr76d5b472014-07-22 15:02:05 -0700133 runShaderTest(reporter, sourceSurface.get(), destinationSurface.get(), info);
piotaixrcef04f82014-07-14 07:48:04 -0700134}
135
136void rasterToGpu(skiatest::Reporter* reporter, GrContext* context) {
137 SkImageInfo info = SkImageInfo::MakeN32Premul(5, 5);
138
piotaixr76d5b472014-07-22 15:02:05 -0700139 SkAutoTUnref<SkSurface> sourceSurface(SkSurface::NewRaster(info));
bsalomonafe30052015-01-16 07:32:33 -0800140 SkAutoTUnref<SkSurface> destinationSurface(SkSurface::NewRenderTarget(context,
141 SkSurface::kNo_Budgeted, info));
piotaixrcef04f82014-07-14 07:48:04 -0700142
piotaixr76d5b472014-07-22 15:02:05 -0700143 runShaderTest(reporter, sourceSurface.get(), destinationSurface.get(), info);
piotaixrcef04f82014-07-14 07:48:04 -0700144}
145
146DEF_GPUTEST(ImageNewShader_GPU, reporter, factory) {
piotaixr76d5b472014-07-22 15:02:05 -0700147 for (int i = 0; i < GrContextFactory::kGLContextTypeCnt; ++i) {
bsalomone904c092014-07-17 10:50:59 -0700148 GrContextFactory::GLContextType glCtxType = (GrContextFactory::GLContextType) i;
piotaixrcef04f82014-07-14 07:48:04 -0700149
bsalomone904c092014-07-17 10:50:59 -0700150 if (!GrContextFactory::IsRenderingGLContext(glCtxType)) {
151 continue;
152 }
piotaixrcef04f82014-07-14 07:48:04 -0700153
bsalomone904c092014-07-17 10:50:59 -0700154 GrContext* context = factory->get(glCtxType);
155
156 if (NULL == context) {
157 continue;
158 }
159
piotaixr76d5b472014-07-22 15:02:05 -0700160 // GPU -> GPU
bsalomone904c092014-07-17 10:50:59 -0700161 gpuToGpu(reporter, context);
162
piotaixr76d5b472014-07-22 15:02:05 -0700163 // GPU -> RASTER
bsalomone904c092014-07-17 10:50:59 -0700164 gpuToRaster(reporter, context);
piotaixrcef04f82014-07-14 07:48:04 -0700165
piotaixr76d5b472014-07-22 15:02:05 -0700166 // RASTER -> GPU
bsalomone904c092014-07-17 10:50:59 -0700167 rasterToGpu(reporter, context);
168 }
piotaixrcef04f82014-07-14 07:48:04 -0700169}
170
171#endif