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
|
|
|
|
|
2014-07-14 14:48:04 +00:00
|
|
|
void testBitmapEquality(skiatest::Reporter* reporter, SkBitmap& bm1, SkBitmap& bm2) {
|
2014-07-22 22:02:05 +00:00
|
|
|
SkAutoLockPixels lockBm1(bm1);
|
|
|
|
SkAutoLockPixels lockBm2(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
|
|
|
}
|
|
|
|
|
|
|
|
void paintSource(SkSurface* sourceSurface) {
|
|
|
|
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
|
|
|
}
|
|
|
|
|
2014-07-22 22:02:05 +00:00
|
|
|
void runShaderTest(skiatest::Reporter* reporter, SkSurface* sourceSurface, SkSurface* destinationSurface, SkImageInfo& info) {
|
|
|
|
paintSource(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
|
|
|
|
2014-10-24 19:54:53 +00:00
|
|
|
SkIRect rect = info.bounds();
|
2014-07-14 14:48:04 +00:00
|
|
|
|
|
|
|
SkBitmap bmOrig;
|
2014-07-22 22:02:05 +00:00
|
|
|
sourceSurface->getCanvas()->readPixels(rect, &bmOrig);
|
|
|
|
|
2014-07-14 14:48:04 +00:00
|
|
|
|
|
|
|
SkBitmap bm;
|
2014-07-22 22:02:05 +00:00
|
|
|
destinationCanvas->readPixels(rect, &bm);
|
2014-07-14 14:48:04 +00:00
|
|
|
|
|
|
|
testBitmapEquality(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;
|
|
|
|
destinationCanvas->readPixels(rect, &bmt);
|
|
|
|
|
|
|
|
// Test correctness
|
|
|
|
{
|
|
|
|
SkAutoLockPixels lockBm(bmt);
|
|
|
|
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);
|
|
|
|
|
2014-07-22 22:02:05 +00:00
|
|
|
SkAutoTUnref<SkSurface> sourceSurface(SkSurface::NewRaster(info));
|
|
|
|
SkAutoTUnref<SkSurface> destinationSurface(SkSurface::NewRaster(info));
|
2014-07-14 14:48:04 +00:00
|
|
|
|
2014-07-22 22:02:05 +00:00
|
|
|
runShaderTest(reporter, sourceSurface.get(), destinationSurface.get(), info);
|
2014-07-14 14:48:04 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#if SK_SUPPORT_GPU
|
|
|
|
|
|
|
|
void gpuToGpu(skiatest::Reporter* reporter, GrContext* context) {
|
|
|
|
SkImageInfo info = SkImageInfo::MakeN32Premul(5, 5);
|
|
|
|
|
2015-01-16 15:32:33 +00:00
|
|
|
SkAutoTUnref<SkSurface> sourceSurface(
|
2016-02-25 16:33:02 +00:00
|
|
|
SkSurface::NewRenderTarget(context, SkBudgeted::kNo, info));
|
2015-01-16 15:32:33 +00:00
|
|
|
SkAutoTUnref<SkSurface> destinationSurface(
|
2016-02-25 16:33:02 +00:00
|
|
|
SkSurface::NewRenderTarget(context, SkBudgeted::kNo, info));
|
2014-07-14 14:48:04 +00:00
|
|
|
|
2014-07-22 22:02:05 +00:00
|
|
|
runShaderTest(reporter, sourceSurface.get(), destinationSurface.get(), info);
|
2014-07-14 14:48:04 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void gpuToRaster(skiatest::Reporter* reporter, GrContext* context) {
|
|
|
|
SkImageInfo info = SkImageInfo::MakeN32Premul(5, 5);
|
|
|
|
|
2015-01-16 15:32:33 +00:00
|
|
|
SkAutoTUnref<SkSurface> sourceSurface(SkSurface::NewRenderTarget(context,
|
2016-02-25 16:33:02 +00:00
|
|
|
SkBudgeted::kNo, info));
|
2014-07-22 22:02:05 +00:00
|
|
|
SkAutoTUnref<SkSurface> destinationSurface(SkSurface::NewRaster(info));
|
2014-07-14 14:48:04 +00:00
|
|
|
|
2014-07-22 22:02:05 +00:00
|
|
|
runShaderTest(reporter, sourceSurface.get(), destinationSurface.get(), info);
|
2014-07-14 14:48:04 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void rasterToGpu(skiatest::Reporter* reporter, GrContext* context) {
|
|
|
|
SkImageInfo info = SkImageInfo::MakeN32Premul(5, 5);
|
|
|
|
|
2014-07-22 22:02:05 +00:00
|
|
|
SkAutoTUnref<SkSurface> sourceSurface(SkSurface::NewRaster(info));
|
2015-01-16 15:32:33 +00:00
|
|
|
SkAutoTUnref<SkSurface> destinationSurface(SkSurface::NewRenderTarget(context,
|
2016-02-25 16:33:02 +00:00
|
|
|
SkBudgeted::kNo, info));
|
2014-07-14 14:48:04 +00:00
|
|
|
|
2014-07-22 22:02:05 +00:00
|
|
|
runShaderTest(reporter, sourceSurface.get(), destinationSurface.get(), info);
|
2014-07-14 14:48:04 +00:00
|
|
|
}
|
|
|
|
|
2015-12-01 12:35:26 +00:00
|
|
|
DEF_GPUTEST_FOR_RENDERING_CONTEXTS(ImageNewShader_GPU, reporter, context) {
|
|
|
|
// GPU -> GPU
|
|
|
|
gpuToGpu(reporter, context);
|
2014-07-14 14:48:04 +00:00
|
|
|
|
2015-12-01 12:35:26 +00:00
|
|
|
// GPU -> RASTER
|
|
|
|
gpuToRaster(reporter, context);
|
2014-07-14 14:48:04 +00:00
|
|
|
|
2015-12-01 12:35:26 +00:00
|
|
|
// RASTER -> GPU
|
|
|
|
rasterToGpu(reporter, context);
|
2014-07-14 14:48:04 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#endif
|