2014-07-14 18:48:04 +04: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.
|
|
|
|
*/
|
|
|
|
|
2014-07-14 21:25:00 +04:00
|
|
|
#include "SkTypes.h"
|
2014-07-14 18:48:04 +04:00
|
|
|
#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) {
|
2014-07-23 02:02:05 +04:00
|
|
|
SkAutoLockPixels lockBm1(bm1);
|
|
|
|
SkAutoLockPixels lockBm2(bm2);
|
2014-07-14 18:48:04 +04:00
|
|
|
|
|
|
|
REPORTER_ASSERT(reporter, bm1.getSize() == bm2.getSize());
|
|
|
|
REPORTER_ASSERT(reporter, 0 == memcmp(bm1.getPixels(), bm2.getPixels(), bm1.getSize()));
|
2014-07-23 02:02:05 +04: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 18:48:04 +04:00
|
|
|
|
2014-07-23 02:02:05 +04:00
|
|
|
SkRect rect = SkRect::MakeXYWH(
|
|
|
|
SkIntToScalar(1),
|
|
|
|
SkIntToScalar(0),
|
|
|
|
SkIntToScalar(1),
|
|
|
|
SkIntToScalar(sourceSurface->height()));
|
|
|
|
|
|
|
|
sourceCanvas->drawRect(rect, paintColor);
|
2014-07-14 18:48:04 +04:00
|
|
|
}
|
|
|
|
|
2014-07-23 02:02:05 +04:00
|
|
|
void runShaderTest(skiatest::Reporter* reporter, SkSurface* sourceSurface, SkSurface* destinationSurface, SkImageInfo& info) {
|
|
|
|
paintSource(sourceSurface);
|
2014-07-14 18:48:04 +04:00
|
|
|
|
2014-07-23 02:02:05 +04:00
|
|
|
SkAutoTUnref<SkImage> sourceImage(sourceSurface->newImageSnapshot());
|
|
|
|
SkAutoTUnref<SkShader> sourceShader(sourceImage->newShader(
|
2014-07-14 18:48:04 +04:00
|
|
|
SkShader::kRepeat_TileMode,
|
|
|
|
SkShader::kRepeat_TileMode));
|
|
|
|
|
|
|
|
SkPaint paint;
|
2014-07-23 02:02:05 +04:00
|
|
|
paint.setShader(sourceShader);
|
|
|
|
|
|
|
|
SkCanvas* destinationCanvas = destinationSurface->getCanvas();
|
|
|
|
destinationCanvas->clear(SK_ColorTRANSPARENT);
|
|
|
|
destinationCanvas->drawPaint(paint);
|
2014-07-14 18:48:04 +04:00
|
|
|
|
2014-07-23 02:02:05 +04:00
|
|
|
SkIRect rect = SkIRect::MakeWH(info.width(), info.height());
|
2014-07-14 18:48:04 +04:00
|
|
|
|
|
|
|
SkBitmap bmOrig;
|
2014-07-23 02:02:05 +04:00
|
|
|
sourceSurface->getCanvas()->readPixels(rect, &bmOrig);
|
|
|
|
|
2014-07-14 18:48:04 +04:00
|
|
|
|
|
|
|
SkBitmap bm;
|
2014-07-23 02:02:05 +04:00
|
|
|
destinationCanvas->readPixels(rect, &bm);
|
2014-07-14 18:48:04 +04:00
|
|
|
|
|
|
|
testBitmapEquality(reporter, bmOrig, bm);
|
2014-07-23 02:02:05 +04:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// Test with a translated shader
|
|
|
|
SkMatrix matrix;
|
|
|
|
matrix.setTranslate(SkIntToScalar(-1), SkIntToScalar(0));
|
|
|
|
|
|
|
|
SkAutoTUnref<SkShader> sourceShaderTranslated(sourceImage->newShader(
|
|
|
|
SkShader::kRepeat_TileMode,
|
|
|
|
SkShader::kRepeat_TileMode,
|
|
|
|
&matrix));
|
|
|
|
|
|
|
|
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 18:48:04 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
DEF_TEST(ImageNewShader, reporter) {
|
|
|
|
SkImageInfo info = SkImageInfo::MakeN32Premul(5, 5);
|
|
|
|
|
2014-07-23 02:02:05 +04:00
|
|
|
SkAutoTUnref<SkSurface> sourceSurface(SkSurface::NewRaster(info));
|
|
|
|
SkAutoTUnref<SkSurface> destinationSurface(SkSurface::NewRaster(info));
|
2014-07-14 18:48:04 +04:00
|
|
|
|
2014-07-23 02:02:05 +04:00
|
|
|
runShaderTest(reporter, sourceSurface.get(), destinationSurface.get(), info);
|
2014-07-14 18:48:04 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
#if SK_SUPPORT_GPU
|
|
|
|
|
|
|
|
void gpuToGpu(skiatest::Reporter* reporter, GrContext* context) {
|
|
|
|
SkImageInfo info = SkImageInfo::MakeN32Premul(5, 5);
|
|
|
|
|
2014-07-23 02:02:05 +04:00
|
|
|
SkAutoTUnref<SkSurface> sourceSurface(SkSurface::NewRenderTarget(context, info));
|
|
|
|
SkAutoTUnref<SkSurface> destinationSurface(SkSurface::NewRenderTarget(context, info));
|
2014-07-14 18:48:04 +04:00
|
|
|
|
2014-07-23 02:02:05 +04:00
|
|
|
runShaderTest(reporter, sourceSurface.get(), destinationSurface.get(), info);
|
2014-07-14 18:48:04 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
void gpuToRaster(skiatest::Reporter* reporter, GrContext* context) {
|
|
|
|
SkImageInfo info = SkImageInfo::MakeN32Premul(5, 5);
|
|
|
|
|
2014-07-23 02:02:05 +04:00
|
|
|
SkAutoTUnref<SkSurface> sourceSurface(SkSurface::NewRenderTarget(context, info));
|
|
|
|
SkAutoTUnref<SkSurface> destinationSurface(SkSurface::NewRaster(info));
|
2014-07-14 18:48:04 +04:00
|
|
|
|
2014-07-23 02:02:05 +04:00
|
|
|
runShaderTest(reporter, sourceSurface.get(), destinationSurface.get(), info);
|
2014-07-14 18:48:04 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
void rasterToGpu(skiatest::Reporter* reporter, GrContext* context) {
|
|
|
|
SkImageInfo info = SkImageInfo::MakeN32Premul(5, 5);
|
|
|
|
|
2014-07-23 02:02:05 +04:00
|
|
|
SkAutoTUnref<SkSurface> sourceSurface(SkSurface::NewRaster(info));
|
|
|
|
SkAutoTUnref<SkSurface> destinationSurface(SkSurface::NewRenderTarget(context, info));
|
2014-07-14 18:48:04 +04:00
|
|
|
|
2014-07-23 02:02:05 +04:00
|
|
|
runShaderTest(reporter, sourceSurface.get(), destinationSurface.get(), info);
|
2014-07-14 18:48:04 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
DEF_GPUTEST(ImageNewShader_GPU, reporter, factory) {
|
2014-07-23 02:02:05 +04:00
|
|
|
for (int i = 0; i < GrContextFactory::kGLContextTypeCnt; ++i) {
|
2014-07-17 21:50:59 +04:00
|
|
|
GrContextFactory::GLContextType glCtxType = (GrContextFactory::GLContextType) i;
|
2014-07-14 18:48:04 +04:00
|
|
|
|
2014-07-17 21:50:59 +04:00
|
|
|
if (!GrContextFactory::IsRenderingGLContext(glCtxType)) {
|
|
|
|
continue;
|
|
|
|
}
|
2014-07-14 18:48:04 +04:00
|
|
|
|
2014-07-17 21:50:59 +04:00
|
|
|
GrContext* context = factory->get(glCtxType);
|
2014-07-14 18:48:04 +04:00
|
|
|
|
2014-07-17 21:50:59 +04:00
|
|
|
if (NULL == context) {
|
|
|
|
continue;
|
|
|
|
}
|
2014-07-14 18:48:04 +04:00
|
|
|
|
2014-07-23 02:02:05 +04:00
|
|
|
// GPU -> GPU
|
2014-07-17 21:50:59 +04:00
|
|
|
gpuToGpu(reporter, context);
|
|
|
|
|
2014-07-23 02:02:05 +04:00
|
|
|
// GPU -> RASTER
|
2014-07-17 21:50:59 +04:00
|
|
|
gpuToRaster(reporter, context);
|
|
|
|
|
2014-07-23 02:02:05 +04:00
|
|
|
// RASTER -> GPU
|
2014-07-17 21:50:59 +04:00
|
|
|
rasterToGpu(reporter, context);
|
|
|
|
}
|
2014-07-14 18:48:04 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
#endif
|