blob: f34d066f6bbf590c557565547e29486c3c0dfa95 [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) {
20 bm1.lockPixels();
21 bm2.lockPixels();
22
23 REPORTER_ASSERT(reporter, bm1.getSize() == bm2.getSize());
24 REPORTER_ASSERT(reporter, 0 == memcmp(bm1.getPixels(), bm2.getPixels(), bm1.getSize()));
25
26 bm2.unlockPixels();
27 bm1.unlockPixels();
28}
29
30void runShaderTest(skiatest::Reporter* reporter, SkSurface* source, SkSurface* destination, SkImageInfo& info) {
31 SkCanvas* rasterCanvas = source->getCanvas();
32 rasterCanvas->drawColor(0xFFDEDEDE, SkXfermode::kSrc_Mode);
33
34 SkAutoTUnref<SkImage> rasterImage(source->newImageSnapshot());
35 SkAutoTUnref<SkShader> rasterShader(rasterImage->newShader(
36 SkShader::kRepeat_TileMode,
37 SkShader::kRepeat_TileMode));
38
39 SkPaint paint;
40 paint.setShader(rasterShader);
41 SkCanvas* canvasDest = destination->getCanvas();
42 canvasDest->clear(SK_ColorTRANSPARENT);
43 canvasDest->drawPaint(paint);
44
45 SkIRect rect = SkIRect::MakeXYWH(0, 0, 5, 5);
46
47 SkBitmap bmOrig;
48 rasterCanvas->readPixels(rect, &bmOrig);
49
50 SkBitmap bm;
51 canvasDest->readPixels(rect, &bm);
52
53 testBitmapEquality(reporter, bmOrig, bm);
54}
55
56DEF_TEST(ImageNewShader, reporter) {
57 SkImageInfo info = SkImageInfo::MakeN32Premul(5, 5);
58
59 SkAutoTUnref<SkSurface> srcSurface(SkSurface::NewRaster(info));
60 SkAutoTUnref<SkSurface> dstSurface(SkSurface::NewRaster(info));
61
62 runShaderTest(reporter, srcSurface.get(), dstSurface.get(), info);
63}
64
65#if SK_SUPPORT_GPU
66
67void gpuToGpu(skiatest::Reporter* reporter, GrContext* context) {
68 SkImageInfo info = SkImageInfo::MakeN32Premul(5, 5);
69
70 SkAutoTUnref<SkSurface> srcSurface(SkSurface::NewRenderTarget(context, info));
71 SkAutoTUnref<SkSurface> dstSurface(SkSurface::NewRenderTarget(context, info));
72
73 runShaderTest(reporter, srcSurface.get(), dstSurface.get(), info);
74}
75
76void gpuToRaster(skiatest::Reporter* reporter, GrContext* context) {
77 SkImageInfo info = SkImageInfo::MakeN32Premul(5, 5);
78
79 SkAutoTUnref<SkSurface> srcSurface(SkSurface::NewRenderTarget(context, info));
80 SkAutoTUnref<SkSurface> dstSurface(SkSurface::NewRaster(info));
81
82 runShaderTest(reporter, srcSurface.get(), dstSurface.get(), info);
83}
84
85void rasterToGpu(skiatest::Reporter* reporter, GrContext* context) {
86 SkImageInfo info = SkImageInfo::MakeN32Premul(5, 5);
87
88 SkAutoTUnref<SkSurface> srcSurface(SkSurface::NewRaster(info));
89 SkAutoTUnref<SkSurface> dstSurface(SkSurface::NewRenderTarget(context, info));
90
91 runShaderTest(reporter, srcSurface.get(), dstSurface.get(), info);
92}
93
94DEF_GPUTEST(ImageNewShader_GPU, reporter, factory) {
bsalomone904c092014-07-17 10:50:59 -070095 for (int i= 0; i < GrContextFactory::kGLContextTypeCnt; ++i) {
96 GrContextFactory::GLContextType glCtxType = (GrContextFactory::GLContextType) i;
piotaixrcef04f82014-07-14 07:48:04 -070097
bsalomone904c092014-07-17 10:50:59 -070098 if (!GrContextFactory::IsRenderingGLContext(glCtxType)) {
99 continue;
100 }
piotaixrcef04f82014-07-14 07:48:04 -0700101
bsalomone904c092014-07-17 10:50:59 -0700102 GrContext* context = factory->get(glCtxType);
103
104 if (NULL == context) {
105 continue;
106 }
107
108 // GPU -> GPU
109 gpuToGpu(reporter, context);
110
111 // GPU -> RASTER
112 gpuToRaster(reporter, context);
piotaixrcef04f82014-07-14 07:48:04 -0700113
114
bsalomone904c092014-07-17 10:50:59 -0700115 // RASTER -> GPU
116 rasterToGpu(reporter, context);
117 }
piotaixrcef04f82014-07-14 07:48:04 -0700118}
119
120#endif