aboutsummaryrefslogtreecommitdiffhomepage
path: root/tests/ImageNewShaderTest.cpp
blob: f34d066f6bbf590c557565547e29486c3c0dfa95 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
/*
 * Copyright 2014 Google Inc.
 *
 * Use of this source code is governed by a BSD-style license that can be
 * found in the LICENSE file.
 */

#include "SkTypes.h"
#if SK_SUPPORT_GPU
#include "GrContextFactory.h"
#endif
#include "SkCanvas.h"
#include "SkImage.h"
#include "SkShader.h"
#include "SkSurface.h"

#include "Test.h"

void testBitmapEquality(skiatest::Reporter* reporter, SkBitmap& bm1, SkBitmap& bm2) {
    bm1.lockPixels();
    bm2.lockPixels();

    REPORTER_ASSERT(reporter, bm1.getSize() == bm2.getSize());
    REPORTER_ASSERT(reporter, 0 == memcmp(bm1.getPixels(), bm2.getPixels(), bm1.getSize()));

    bm2.unlockPixels();
    bm1.unlockPixels();
}

void runShaderTest(skiatest::Reporter* reporter, SkSurface* source, SkSurface* destination, SkImageInfo& info) {
    SkCanvas* rasterCanvas = source->getCanvas();
    rasterCanvas->drawColor(0xFFDEDEDE, SkXfermode::kSrc_Mode);

    SkAutoTUnref<SkImage> rasterImage(source->newImageSnapshot());
    SkAutoTUnref<SkShader> rasterShader(rasterImage->newShader(
            SkShader::kRepeat_TileMode,
            SkShader::kRepeat_TileMode));

    SkPaint paint;
    paint.setShader(rasterShader);
    SkCanvas* canvasDest = destination->getCanvas();
    canvasDest->clear(SK_ColorTRANSPARENT);
    canvasDest->drawPaint(paint);

    SkIRect rect = SkIRect::MakeXYWH(0, 0, 5, 5);

    SkBitmap bmOrig;
    rasterCanvas->readPixels(rect, &bmOrig);

    SkBitmap bm;
    canvasDest->readPixels(rect, &bm);

    testBitmapEquality(reporter, bmOrig, bm);
}

DEF_TEST(ImageNewShader, reporter) {
    SkImageInfo info = SkImageInfo::MakeN32Premul(5, 5);

    SkAutoTUnref<SkSurface> srcSurface(SkSurface::NewRaster(info));
    SkAutoTUnref<SkSurface> dstSurface(SkSurface::NewRaster(info));

    runShaderTest(reporter, srcSurface.get(), dstSurface.get(), info);
}

#if SK_SUPPORT_GPU

void gpuToGpu(skiatest::Reporter* reporter, GrContext* context) {
    SkImageInfo info = SkImageInfo::MakeN32Premul(5, 5);

    SkAutoTUnref<SkSurface> srcSurface(SkSurface::NewRenderTarget(context, info));
    SkAutoTUnref<SkSurface> dstSurface(SkSurface::NewRenderTarget(context, info));

    runShaderTest(reporter, srcSurface.get(), dstSurface.get(), info);
}

void gpuToRaster(skiatest::Reporter* reporter, GrContext* context) {
    SkImageInfo info = SkImageInfo::MakeN32Premul(5, 5);

    SkAutoTUnref<SkSurface> srcSurface(SkSurface::NewRenderTarget(context, info));
    SkAutoTUnref<SkSurface> dstSurface(SkSurface::NewRaster(info));

    runShaderTest(reporter, srcSurface.get(), dstSurface.get(), info);
}

void rasterToGpu(skiatest::Reporter* reporter, GrContext* context) {
    SkImageInfo info = SkImageInfo::MakeN32Premul(5, 5);

    SkAutoTUnref<SkSurface> srcSurface(SkSurface::NewRaster(info));
    SkAutoTUnref<SkSurface> dstSurface(SkSurface::NewRenderTarget(context, info));

    runShaderTest(reporter, srcSurface.get(), dstSurface.get(), info);
}

DEF_GPUTEST(ImageNewShader_GPU, reporter, factory) {
    for (int i= 0; i < GrContextFactory::kGLContextTypeCnt; ++i) {
        GrContextFactory::GLContextType glCtxType = (GrContextFactory::GLContextType) i;

        if (!GrContextFactory::IsRenderingGLContext(glCtxType)) {
            continue;
        }

        GrContext* context = factory->get(glCtxType);

        if (NULL == context) {
            continue;
        }

        // GPU -> GPU
        gpuToGpu(reporter, context);

        // GPU -> RASTER
        gpuToRaster(reporter, context);


        // RASTER -> GPU
        rasterToGpu(reporter, context);
    }
}

#endif