2 * Copyright 2014 Google Inc.
4 * Use of this source code is governed by a BSD-style license that can be
5 * found in the LICENSE file.
10 #include "GrContextFactory.h"
15 #include "SkSurface.h"
19 void testBitmapEquality(skiatest::Reporter* reporter, SkBitmap& bm1, SkBitmap& bm2) {
20 SkAutoLockPixels lockBm1(bm1);
21 SkAutoLockPixels lockBm2(bm2);
23 REPORTER_ASSERT(reporter, bm1.getSize() == bm2.getSize());
24 REPORTER_ASSERT(reporter, 0 == memcmp(bm1.getPixels(), bm2.getPixels(), bm1.getSize()));
27 void paintSource(SkSurface* sourceSurface) {
28 SkCanvas* sourceCanvas = sourceSurface->getCanvas();
29 sourceCanvas->clear(0xFFDEDEDE);
32 paintColor.setColor(0xFFFF0000);
33 paintColor.setStyle(SkPaint::kFill_Style);
35 SkRect rect = SkRect::MakeXYWH(
39 SkIntToScalar(sourceSurface->height()));
41 sourceCanvas->drawRect(rect, paintColor);
44 void runShaderTest(skiatest::Reporter* reporter, SkSurface* sourceSurface, SkSurface* destinationSurface, SkImageInfo& info) {
45 paintSource(sourceSurface);
47 SkAutoTUnref<SkImage> sourceImage(sourceSurface->newImageSnapshot());
48 SkAutoTUnref<SkShader> sourceShader(sourceImage->newShader(
49 SkShader::kRepeat_TileMode,
50 SkShader::kRepeat_TileMode));
53 paint.setShader(sourceShader);
55 SkCanvas* destinationCanvas = destinationSurface->getCanvas();
56 destinationCanvas->clear(SK_ColorTRANSPARENT);
57 destinationCanvas->drawPaint(paint);
59 SkIRect rect = info.bounds();
62 sourceSurface->getCanvas()->readPixels(rect, &bmOrig);
66 destinationCanvas->readPixels(rect, &bm);
68 testBitmapEquality(reporter, bmOrig, bm);
72 // Test with a translated shader
74 matrix.setTranslate(SkIntToScalar(-1), SkIntToScalar(0));
76 SkAutoTUnref<SkShader> sourceShaderTranslated(sourceImage->newShader(
77 SkShader::kRepeat_TileMode,
78 SkShader::kRepeat_TileMode,
81 destinationCanvas->clear(SK_ColorTRANSPARENT);
83 SkPaint paintTranslated;
84 paintTranslated.setShader(sourceShaderTranslated);
86 destinationCanvas->drawPaint(paintTranslated);
89 destinationCanvas->readPixels(rect, &bmt);
93 SkAutoLockPixels lockBm(bmt);
94 for (int y = 0; y < info.height(); y++) {
95 REPORTER_ASSERT(reporter, 0xFFFF0000 == bmt.getColor(0, y));
97 for (int x = 1; x < info.width(); x++) {
98 REPORTER_ASSERT(reporter, 0xFFDEDEDE == bmt.getColor(x, y));
104 DEF_TEST(ImageNewShader, reporter) {
105 SkImageInfo info = SkImageInfo::MakeN32Premul(5, 5);
107 SkAutoTUnref<SkSurface> sourceSurface(SkSurface::NewRaster(info));
108 SkAutoTUnref<SkSurface> destinationSurface(SkSurface::NewRaster(info));
110 runShaderTest(reporter, sourceSurface.get(), destinationSurface.get(), info);
115 void gpuToGpu(skiatest::Reporter* reporter, GrContext* context) {
116 SkImageInfo info = SkImageInfo::MakeN32Premul(5, 5);
118 SkAutoTUnref<SkSurface> sourceSurface(SkSurface::NewRenderTarget(context, info));
119 SkAutoTUnref<SkSurface> destinationSurface(SkSurface::NewRenderTarget(context, info));
121 runShaderTest(reporter, sourceSurface.get(), destinationSurface.get(), info);
124 void gpuToRaster(skiatest::Reporter* reporter, GrContext* context) {
125 SkImageInfo info = SkImageInfo::MakeN32Premul(5, 5);
127 SkAutoTUnref<SkSurface> sourceSurface(SkSurface::NewRenderTarget(context, info));
128 SkAutoTUnref<SkSurface> destinationSurface(SkSurface::NewRaster(info));
130 runShaderTest(reporter, sourceSurface.get(), destinationSurface.get(), info);
133 void rasterToGpu(skiatest::Reporter* reporter, GrContext* context) {
134 SkImageInfo info = SkImageInfo::MakeN32Premul(5, 5);
136 SkAutoTUnref<SkSurface> sourceSurface(SkSurface::NewRaster(info));
137 SkAutoTUnref<SkSurface> destinationSurface(SkSurface::NewRenderTarget(context, info));
139 runShaderTest(reporter, sourceSurface.get(), destinationSurface.get(), info);
142 DEF_GPUTEST(ImageNewShader_GPU, reporter, factory) {
143 for (int i = 0; i < GrContextFactory::kGLContextTypeCnt; ++i) {
144 GrContextFactory::GLContextType glCtxType = (GrContextFactory::GLContextType) i;
146 if (!GrContextFactory::IsRenderingGLContext(glCtxType)) {
150 GrContext* context = factory->get(glCtxType);
152 if (NULL == context) {
157 gpuToGpu(reporter, context);
160 gpuToRaster(reporter, context);
163 rasterToGpu(reporter, context);