2014-07-14 14:48:04 +00:00
|
|
|
/*
|
|
|
|
* Copyright 2014 Google Inc.
|
|
|
|
*
|
|
|
|
* Use of this source code is governed by a BSD-style license that can be
|
|
|
|
* found in the LICENSE file.
|
|
|
|
*/
|
|
|
|
|
2020-12-23 15:11:33 +00:00
|
|
|
#include "include/core/SkBitmap.h"
|
2019-04-23 17:05:21 +00:00
|
|
|
#include "include/core/SkCanvas.h"
|
|
|
|
#include "include/core/SkImage.h"
|
|
|
|
#include "include/core/SkShader.h"
|
|
|
|
#include "include/core/SkSurface.h"
|
|
|
|
#include "include/core/SkTypes.h"
|
2020-07-06 14:56:46 +00:00
|
|
|
#include "include/gpu/GrDirectContext.h"
|
2019-04-23 17:05:21 +00:00
|
|
|
#include "tests/Test.h"
|
2021-11-29 17:27:00 +00:00
|
|
|
#include "tools/Resources.h"
|
2014-07-14 14:48:04 +00:00
|
|
|
|
2017-01-20 17:44:06 +00:00
|
|
|
static void test_bitmap_equality(skiatest::Reporter* reporter, SkBitmap& bm1, SkBitmap& bm2) {
|
2017-10-03 18:47:21 +00:00
|
|
|
REPORTER_ASSERT(reporter, bm1.computeByteSize() == bm2.computeByteSize());
|
|
|
|
REPORTER_ASSERT(reporter, 0 == memcmp(bm1.getPixels(), bm2.getPixels(), bm1.computeByteSize()));
|
2014-07-22 22:02:05 +00:00
|
|
|
}
|
|
|
|
|
2017-01-20 17:44:06 +00:00
|
|
|
static void paint_source(SkSurface* sourceSurface) {
|
2014-07-22 22:02:05 +00:00
|
|
|
SkCanvas* sourceCanvas = sourceSurface->getCanvas();
|
|
|
|
sourceCanvas->clear(0xFFDEDEDE);
|
|
|
|
|
|
|
|
SkPaint paintColor;
|
|
|
|
paintColor.setColor(0xFFFF0000);
|
|
|
|
paintColor.setStyle(SkPaint::kFill_Style);
|
2014-07-14 14:48:04 +00:00
|
|
|
|
2014-07-22 22:02:05 +00:00
|
|
|
SkRect rect = SkRect::MakeXYWH(
|
|
|
|
SkIntToScalar(1),
|
|
|
|
SkIntToScalar(0),
|
|
|
|
SkIntToScalar(1),
|
|
|
|
SkIntToScalar(sourceSurface->height()));
|
|
|
|
|
|
|
|
sourceCanvas->drawRect(rect, paintColor);
|
2014-07-14 14:48:04 +00:00
|
|
|
}
|
|
|
|
|
2017-01-20 17:44:06 +00:00
|
|
|
static void run_shader_test(skiatest::Reporter* reporter, SkSurface* sourceSurface,
|
|
|
|
SkSurface* destinationSurface, SkImageInfo& info) {
|
|
|
|
paint_source(sourceSurface);
|
2014-07-14 14:48:04 +00:00
|
|
|
|
2016-03-17 17:51:11 +00:00
|
|
|
sk_sp<SkImage> sourceImage(sourceSurface->makeImageSnapshot());
|
2016-03-09 22:47:34 +00:00
|
|
|
sk_sp<SkShader> sourceShader = sourceImage->makeShader(
|
2020-12-09 02:58:35 +00:00
|
|
|
SkTileMode::kRepeat, SkTileMode::kRepeat, SkSamplingOptions());
|
2014-07-14 14:48:04 +00:00
|
|
|
|
|
|
|
SkPaint paint;
|
2014-07-22 22:02:05 +00:00
|
|
|
paint.setShader(sourceShader);
|
|
|
|
|
|
|
|
SkCanvas* destinationCanvas = destinationSurface->getCanvas();
|
|
|
|
destinationCanvas->clear(SK_ColorTRANSPARENT);
|
|
|
|
destinationCanvas->drawPaint(paint);
|
2014-07-14 14:48:04 +00:00
|
|
|
|
|
|
|
SkBitmap bmOrig;
|
2017-04-17 14:53:29 +00:00
|
|
|
bmOrig.allocN32Pixels(info.width(), info.height());
|
2017-07-21 18:24:29 +00:00
|
|
|
sourceSurface->readPixels(bmOrig, 0, 0);
|
2014-07-22 22:02:05 +00:00
|
|
|
|
2014-07-14 14:48:04 +00:00
|
|
|
|
|
|
|
SkBitmap bm;
|
2017-04-17 14:53:29 +00:00
|
|
|
bm.allocN32Pixels(info.width(), info.height());
|
2017-07-21 18:24:29 +00:00
|
|
|
destinationSurface->readPixels(bm, 0, 0);
|
2014-07-14 14:48:04 +00:00
|
|
|
|
2017-01-20 17:44:06 +00:00
|
|
|
test_bitmap_equality(reporter, bmOrig, bm);
|
2014-07-22 22:02:05 +00:00
|
|
|
|
|
|
|
// Test with a translated shader
|
|
|
|
SkMatrix matrix;
|
|
|
|
matrix.setTranslate(SkIntToScalar(-1), SkIntToScalar(0));
|
|
|
|
|
2016-03-09 22:47:34 +00:00
|
|
|
sk_sp<SkShader> sourceShaderTranslated = sourceImage->makeShader(
|
2019-04-03 14:27:45 +00:00
|
|
|
SkTileMode::kRepeat,
|
|
|
|
SkTileMode::kRepeat,
|
2020-12-09 02:58:35 +00:00
|
|
|
SkSamplingOptions(), &matrix);
|
2014-07-22 22:02:05 +00:00
|
|
|
|
|
|
|
destinationCanvas->clear(SK_ColorTRANSPARENT);
|
|
|
|
|
|
|
|
SkPaint paintTranslated;
|
|
|
|
paintTranslated.setShader(sourceShaderTranslated);
|
|
|
|
|
|
|
|
destinationCanvas->drawPaint(paintTranslated);
|
|
|
|
|
|
|
|
SkBitmap bmt;
|
2017-04-17 14:53:29 +00:00
|
|
|
bmt.allocN32Pixels(info.width(), info.height());
|
2017-07-21 18:24:29 +00:00
|
|
|
destinationSurface->readPixels(bmt, 0, 0);
|
2014-07-22 22:02:05 +00:00
|
|
|
|
|
|
|
// Test correctness
|
|
|
|
{
|
|
|
|
for (int y = 0; y < info.height(); y++) {
|
|
|
|
REPORTER_ASSERT(reporter, 0xFFFF0000 == bmt.getColor(0, y));
|
|
|
|
|
|
|
|
for (int x = 1; x < info.width(); x++) {
|
|
|
|
REPORTER_ASSERT(reporter, 0xFFDEDEDE == bmt.getColor(x, y));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2014-07-14 14:48:04 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
DEF_TEST(ImageNewShader, reporter) {
|
|
|
|
SkImageInfo info = SkImageInfo::MakeN32Premul(5, 5);
|
|
|
|
|
2016-03-24 01:59:25 +00:00
|
|
|
auto sourceSurface(SkSurface::MakeRaster(info));
|
|
|
|
auto destinationSurface(SkSurface::MakeRaster(info));
|
2014-07-14 14:48:04 +00:00
|
|
|
|
2017-01-20 17:44:06 +00:00
|
|
|
run_shader_test(reporter, sourceSurface.get(), destinationSurface.get(), info);
|
2014-07-14 14:48:04 +00:00
|
|
|
}
|
|
|
|
|
2020-07-22 13:35:49 +00:00
|
|
|
static void gpu_to_gpu(skiatest::Reporter* reporter, GrRecordingContext* rContext) {
|
2014-07-14 14:48:04 +00:00
|
|
|
SkImageInfo info = SkImageInfo::MakeN32Premul(5, 5);
|
|
|
|
|
2020-07-22 13:35:49 +00:00
|
|
|
auto sourceSurface(SkSurface::MakeRenderTarget(rContext, SkBudgeted::kNo, info));
|
|
|
|
auto destinationSurface(SkSurface::MakeRenderTarget(rContext, SkBudgeted::kNo, info));
|
2014-07-14 14:48:04 +00:00
|
|
|
|
2017-01-20 17:44:06 +00:00
|
|
|
run_shader_test(reporter, sourceSurface.get(), destinationSurface.get(), info);
|
2014-07-14 14:48:04 +00:00
|
|
|
}
|
|
|
|
|
2020-07-22 13:35:49 +00:00
|
|
|
static void raster_to_gpu(skiatest::Reporter* reporter, GrRecordingContext* rContext) {
|
2014-07-14 14:48:04 +00:00
|
|
|
SkImageInfo info = SkImageInfo::MakeN32Premul(5, 5);
|
|
|
|
|
2016-03-24 01:59:25 +00:00
|
|
|
auto sourceSurface(SkSurface::MakeRaster(info));
|
2020-07-22 13:35:49 +00:00
|
|
|
auto destinationSurface(SkSurface::MakeRenderTarget(rContext, SkBudgeted::kNo, info));
|
2014-07-14 14:48:04 +00:00
|
|
|
|
2017-01-20 17:44:06 +00:00
|
|
|
run_shader_test(reporter, sourceSurface.get(), destinationSurface.get(), info);
|
2014-07-14 14:48:04 +00:00
|
|
|
}
|
|
|
|
|
2016-04-12 16:59:58 +00:00
|
|
|
DEF_GPUTEST_FOR_RENDERING_CONTEXTS(ImageNewShader_GPU, reporter, ctxInfo) {
|
2020-07-22 13:35:49 +00:00
|
|
|
auto dContext = ctxInfo.directContext();
|
2020-07-06 14:56:46 +00:00
|
|
|
|
2015-12-01 12:35:26 +00:00
|
|
|
// GPU -> GPU
|
2020-07-22 13:35:49 +00:00
|
|
|
gpu_to_gpu(reporter, dContext);
|
2014-07-14 14:48:04 +00:00
|
|
|
|
2020-08-27 16:44:07 +00:00
|
|
|
// GPU -> RASTER not currently supported
|
2014-07-14 14:48:04 +00:00
|
|
|
|
2015-12-01 12:35:26 +00:00
|
|
|
// RASTER -> GPU
|
2020-07-22 13:35:49 +00:00
|
|
|
raster_to_gpu(reporter, dContext);
|
2014-07-14 14:48:04 +00:00
|
|
|
}
|
2021-11-29 17:27:00 +00:00
|
|
|
|
|
|
|
DEF_TEST(ImageRawShader, reporter) {
|
|
|
|
auto image = GetResourceAsImage("images/mandrill_32.png");
|
|
|
|
REPORTER_ASSERT(reporter, image);
|
|
|
|
|
|
|
|
// We should be able to turn this into a "raw" image shader:
|
|
|
|
REPORTER_ASSERT(reporter, image->makeRawShader(SkSamplingOptions{}));
|
|
|
|
|
|
|
|
// ... but not if we request cubic filtering
|
|
|
|
REPORTER_ASSERT(reporter,
|
|
|
|
!image->makeRawShader(SkSamplingOptions{SkCubicResampler::Mitchell()}));
|
|
|
|
}
|