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.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "SkCanvas.h"
|
|
|
|
#include "SkImage.h"
|
|
|
|
#include "SkShader.h"
|
|
|
|
#include "SkSurface.h"
|
2015-12-01 12:35:26 +00:00
|
|
|
#include "SkTypes.h"
|
2014-07-14 14:48:04 +00:00
|
|
|
#include "Test.h"
|
|
|
|
|
2015-12-01 12:35:26 +00:00
|
|
|
#if SK_SUPPORT_GPU
|
|
|
|
#include "GrContext.h"
|
|
|
|
#endif
|
|
|
|
|
2017-01-20 17:44:06 +00:00
|
|
|
static void test_bitmap_equality(skiatest::Reporter* reporter, SkBitmap& bm1, SkBitmap& bm2) {
|
2014-07-14 14:48:04 +00:00
|
|
|
REPORTER_ASSERT(reporter, bm1.getSize() == bm2.getSize());
|
|
|
|
REPORTER_ASSERT(reporter, 0 == memcmp(bm1.getPixels(), bm2.getPixels(), bm1.getSize()));
|
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(
|
2014-07-14 14:48:04 +00:00
|
|
|
SkShader::kRepeat_TileMode,
|
2016-03-09 22:47:34 +00:00
|
|
|
SkShader::kRepeat_TileMode);
|
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(
|
2014-07-22 22:02:05 +00:00
|
|
|
SkShader::kRepeat_TileMode,
|
|
|
|
SkShader::kRepeat_TileMode,
|
2016-03-09 22:47:34 +00:00
|
|
|
&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
|
|
|
}
|
|
|
|
|
|
|
|
#if SK_SUPPORT_GPU
|
|
|
|
|
2017-01-20 17:44:06 +00:00
|
|
|
static void gpu_to_gpu(skiatest::Reporter* reporter, GrContext* context) {
|
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::MakeRenderTarget(context, SkBudgeted::kNo, info));
|
|
|
|
auto destinationSurface(SkSurface::MakeRenderTarget(context, 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
|
|
|
}
|
|
|
|
|
2017-01-20 17:44:06 +00:00
|
|
|
static void gpu_to_raster(skiatest::Reporter* reporter, GrContext* context) {
|
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::MakeRenderTarget(context, SkBudgeted::kNo, 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
|
|
|
}
|
|
|
|
|
2017-01-20 17:44:06 +00:00
|
|
|
static void raster_to_gpu(skiatest::Reporter* reporter, GrContext* context) {
|
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));
|
|
|
|
auto destinationSurface(SkSurface::MakeRenderTarget(context, 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) {
|
2015-12-01 12:35:26 +00:00
|
|
|
// GPU -> GPU
|
2017-01-20 17:44:06 +00:00
|
|
|
gpu_to_gpu(reporter, ctxInfo.grContext());
|
2014-07-14 14:48:04 +00:00
|
|
|
|
2015-12-01 12:35:26 +00:00
|
|
|
// GPU -> RASTER
|
2017-01-20 17:44:06 +00:00
|
|
|
gpu_to_raster(reporter, ctxInfo.grContext());
|
2014-07-14 14:48:04 +00:00
|
|
|
|
2015-12-01 12:35:26 +00:00
|
|
|
// RASTER -> GPU
|
2017-01-20 17:44:06 +00:00
|
|
|
raster_to_gpu(reporter, ctxInfo.grContext());
|
2014-07-14 14:48:04 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#endif
|