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 "include/core/SkBitmap.h"
9#include "include/core/SkCanvas.h"
10#include "include/core/SkImage.h"
11#include "include/core/SkShader.h"
12#include "include/core/SkSurface.h"
13#include "include/core/SkTypes.h"
14#include "include/gpu/GrDirectContext.h"
15#include "tests/Test.h"
16
17static void test_bitmap_equality(skiatest::Reporter* reporter, SkBitmap& bm1, SkBitmap& bm2) {
18    REPORTER_ASSERT(reporter, bm1.computeByteSize() == bm2.computeByteSize());
19    REPORTER_ASSERT(reporter, 0 == memcmp(bm1.getPixels(), bm2.getPixels(), bm1.computeByteSize()));
20}
21
22static void paint_source(SkSurface* sourceSurface) {
23    SkCanvas* sourceCanvas = sourceSurface->getCanvas();
24    sourceCanvas->clear(0xFFDEDEDE);
25
26    SkPaint paintColor;
27    paintColor.setColor(0xFFFF0000);
28    paintColor.setStyle(SkPaint::kFill_Style);
29
30    SkRect rect = SkRect::MakeXYWH(
31            SkIntToScalar(1),
32            SkIntToScalar(0),
33            SkIntToScalar(1),
34            SkIntToScalar(sourceSurface->height()));
35
36    sourceCanvas->drawRect(rect, paintColor);
37}
38
39static void run_shader_test(skiatest::Reporter* reporter, SkSurface* sourceSurface,
40                            SkSurface* destinationSurface, SkImageInfo& info) {
41    paint_source(sourceSurface);
42
43    sk_sp<SkImage> sourceImage(sourceSurface->makeImageSnapshot());
44    sk_sp<SkShader> sourceShader = sourceImage->makeShader(
45            SkTileMode::kRepeat, SkTileMode::kRepeat, SkSamplingOptions());
46
47    SkPaint paint;
48    paint.setShader(sourceShader);
49
50    SkCanvas* destinationCanvas = destinationSurface->getCanvas();
51    destinationCanvas->clear(SK_ColorTRANSPARENT);
52    destinationCanvas->drawPaint(paint);
53
54    SkBitmap bmOrig;
55    bmOrig.allocN32Pixels(info.width(), info.height());
56    sourceSurface->readPixels(bmOrig, 0, 0);
57
58
59    SkBitmap bm;
60    bm.allocN32Pixels(info.width(), info.height());
61    destinationSurface->readPixels(bm, 0, 0);
62
63    test_bitmap_equality(reporter, bmOrig, bm);
64
65    // Test with a translated shader
66    SkMatrix matrix;
67    matrix.setTranslate(SkIntToScalar(-1), SkIntToScalar(0));
68
69    sk_sp<SkShader> sourceShaderTranslated = sourceImage->makeShader(
70            SkTileMode::kRepeat,
71            SkTileMode::kRepeat,
72            SkSamplingOptions(), &matrix);
73
74    destinationCanvas->clear(SK_ColorTRANSPARENT);
75
76    SkPaint paintTranslated;
77    paintTranslated.setShader(sourceShaderTranslated);
78
79    destinationCanvas->drawPaint(paintTranslated);
80
81    SkBitmap bmt;
82    bmt.allocN32Pixels(info.width(), info.height());
83    destinationSurface->readPixels(bmt, 0, 0);
84
85    //  Test correctness
86    {
87        for (int y = 0; y < info.height(); y++) {
88            REPORTER_ASSERT(reporter, 0xFFFF0000 == bmt.getColor(0, y));
89
90            for (int x = 1; x < info.width(); x++) {
91                REPORTER_ASSERT(reporter, 0xFFDEDEDE == bmt.getColor(x, y));
92            }
93        }
94    }
95}
96
97DEF_TEST(ImageNewShader, reporter) {
98    SkImageInfo info = SkImageInfo::MakeN32Premul(5, 5);
99
100    auto sourceSurface(SkSurface::MakeRaster(info));
101    auto destinationSurface(SkSurface::MakeRaster(info));
102
103    run_shader_test(reporter, sourceSurface.get(), destinationSurface.get(), info);
104}
105
106static void gpu_to_gpu(skiatest::Reporter* reporter, GrRecordingContext* rContext) {
107    SkImageInfo info = SkImageInfo::MakeN32Premul(5, 5);
108
109    auto sourceSurface(SkSurface::MakeRenderTarget(rContext, SkBudgeted::kNo, info));
110    auto destinationSurface(SkSurface::MakeRenderTarget(rContext, SkBudgeted::kNo, info));
111
112    run_shader_test(reporter, sourceSurface.get(), destinationSurface.get(), info);
113}
114
115static void raster_to_gpu(skiatest::Reporter* reporter, GrRecordingContext* rContext) {
116    SkImageInfo info = SkImageInfo::MakeN32Premul(5, 5);
117
118    auto sourceSurface(SkSurface::MakeRaster(info));
119    auto destinationSurface(SkSurface::MakeRenderTarget(rContext, SkBudgeted::kNo, info));
120
121    run_shader_test(reporter, sourceSurface.get(), destinationSurface.get(), info);
122}
123
124DEF_GPUTEST_FOR_RENDERING_CONTEXTS(ImageNewShader_GPU, reporter, ctxInfo) {
125    auto dContext = ctxInfo.directContext();
126
127    //  GPU -> GPU
128    gpu_to_gpu(reporter, dContext);
129
130    //  GPU -> RASTER not currently supported
131
132    //  RASTER -> GPU
133    raster_to_gpu(reporter, dContext);
134}
135