Home | History | Annotate | Download | only in tests
      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 "SkCanvas.h"
      9 #include "SkImage.h"
     10 #include "SkShader.h"
     11 #include "SkSurface.h"
     12 #include "SkTypes.h"
     13 #include "Test.h"
     14 
     15 #include "GrContext.h"
     16 
     17 static 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 
     22 static 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 
     39 static 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             SkShader::kRepeat_TileMode,
     46             SkShader::kRepeat_TileMode);
     47 
     48     SkPaint paint;
     49     paint.setShader(sourceShader);
     50 
     51     SkCanvas* destinationCanvas = destinationSurface->getCanvas();
     52     destinationCanvas->clear(SK_ColorTRANSPARENT);
     53     destinationCanvas->drawPaint(paint);
     54 
     55     SkBitmap bmOrig;
     56     bmOrig.allocN32Pixels(info.width(), info.height());
     57     sourceSurface->readPixels(bmOrig, 0, 0);
     58 
     59 
     60     SkBitmap bm;
     61     bm.allocN32Pixels(info.width(), info.height());
     62     destinationSurface->readPixels(bm, 0, 0);
     63 
     64     test_bitmap_equality(reporter, bmOrig, bm);
     65 
     66     // Test with a translated shader
     67     SkMatrix matrix;
     68     matrix.setTranslate(SkIntToScalar(-1), SkIntToScalar(0));
     69 
     70     sk_sp<SkShader> sourceShaderTranslated = sourceImage->makeShader(
     71             SkShader::kRepeat_TileMode,
     72             SkShader::kRepeat_TileMode,
     73             &matrix);
     74 
     75     destinationCanvas->clear(SK_ColorTRANSPARENT);
     76 
     77     SkPaint paintTranslated;
     78     paintTranslated.setShader(sourceShaderTranslated);
     79 
     80     destinationCanvas->drawPaint(paintTranslated);
     81 
     82     SkBitmap bmt;
     83     bmt.allocN32Pixels(info.width(), info.height());
     84     destinationSurface->readPixels(bmt, 0, 0);
     85 
     86     //  Test correctness
     87     {
     88         for (int y = 0; y < info.height(); y++) {
     89             REPORTER_ASSERT(reporter, 0xFFFF0000 == bmt.getColor(0, y));
     90 
     91             for (int x = 1; x < info.width(); x++) {
     92                 REPORTER_ASSERT(reporter, 0xFFDEDEDE == bmt.getColor(x, y));
     93             }
     94         }
     95     }
     96 }
     97 
     98 DEF_TEST(ImageNewShader, reporter) {
     99     SkImageInfo info = SkImageInfo::MakeN32Premul(5, 5);
    100 
    101     auto sourceSurface(SkSurface::MakeRaster(info));
    102     auto destinationSurface(SkSurface::MakeRaster(info));
    103 
    104     run_shader_test(reporter, sourceSurface.get(), destinationSurface.get(), info);
    105 }
    106 
    107 static void gpu_to_gpu(skiatest::Reporter* reporter, GrContext* context) {
    108     SkImageInfo info = SkImageInfo::MakeN32Premul(5, 5);
    109 
    110     auto sourceSurface(SkSurface::MakeRenderTarget(context, SkBudgeted::kNo, info));
    111     auto destinationSurface(SkSurface::MakeRenderTarget(context, SkBudgeted::kNo, info));
    112 
    113     run_shader_test(reporter, sourceSurface.get(), destinationSurface.get(), info);
    114 }
    115 
    116 static void gpu_to_raster(skiatest::Reporter* reporter, GrContext* context) {
    117     SkImageInfo info = SkImageInfo::MakeN32Premul(5, 5);
    118 
    119     auto sourceSurface(SkSurface::MakeRenderTarget(context, SkBudgeted::kNo, info));
    120     auto destinationSurface(SkSurface::MakeRaster(info));
    121 
    122     run_shader_test(reporter, sourceSurface.get(), destinationSurface.get(), info);
    123 }
    124 
    125 static void raster_to_gpu(skiatest::Reporter* reporter, GrContext* context) {
    126     SkImageInfo info = SkImageInfo::MakeN32Premul(5, 5);
    127 
    128     auto sourceSurface(SkSurface::MakeRaster(info));
    129     auto destinationSurface(SkSurface::MakeRenderTarget(context, SkBudgeted::kNo, info));
    130 
    131     run_shader_test(reporter, sourceSurface.get(), destinationSurface.get(), info);
    132 }
    133 
    134 DEF_GPUTEST_FOR_RENDERING_CONTEXTS(ImageNewShader_GPU, reporter, ctxInfo) {
    135     //  GPU -> GPU
    136     gpu_to_gpu(reporter, ctxInfo.grContext());
    137 
    138     //  GPU -> RASTER
    139     gpu_to_raster(reporter, ctxInfo.grContext());
    140 
    141     //  RASTER -> GPU
    142     raster_to_gpu(reporter, ctxInfo.grContext());
    143 }
    144