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 "Test.h"
      9 
     10 #include "../include/core/SkCanvas.h"
     11 #include "../include/core/SkPicture.h"
     12 #include "../include/core/SkStream.h"
     13 #include "../include/core/SkString.h"
     14 #include "../include/core/SkPictureRecorder.h"
     15 #include "../src/core/SkBlendModePriv.h"
     16 #include <cstring>
     17 
     18 // Verify that replay of a recording into a clipped canvas
     19 // produces the correct bitmap.
     20 // This arose from http://crbug.com/401593 which has
     21 // https://code.google.com/p/skia/issues/detail?id=1291 as its root cause.
     22 
     23 namespace {
     24 
     25 class Drawer {
     26  public:
     27     explicit Drawer() : fImageInfo(SkImageInfo::MakeN32Premul(200, 100)) {
     28         fCircleBM.allocPixels(SkImageInfo::MakeN32Premul(100, 100));
     29         SkCanvas canvas(fCircleBM);
     30         canvas.clear(0xffffffff);
     31         SkPaint circlePaint;
     32         circlePaint.setColor(0xff000000);
     33         canvas.drawCircle(50, 50, 50, circlePaint);
     34     }
     35 
     36     const SkImageInfo& imageInfo() const { return fImageInfo; }
     37 
     38     void draw(SkCanvas* canvas, const SkRect& clipRect, SkBlendMode mode) const {
     39         SkPaint greenPaint;
     40         greenPaint.setColor(0xff008000);
     41         SkPaint blackPaint;
     42         blackPaint.setColor(0xff000000);
     43         SkPaint whitePaint;
     44         whitePaint.setColor(0xffffffff);
     45         SkPaint layerPaint;
     46         layerPaint.setColor(0xff000000);
     47         layerPaint.setBlendMode(mode);
     48         SkRect canvasRect(SkRect::MakeWH(SkIntToScalar(fImageInfo.width()),
     49                                          SkIntToScalar(fImageInfo.height())));
     50 
     51         canvas->clipRect(clipRect);
     52         canvas->clear(0xff000000);
     53 
     54         canvas->saveLayer(nullptr, &blackPaint);
     55             canvas->drawRect(canvasRect, greenPaint);
     56             canvas->saveLayer(nullptr, &layerPaint);
     57                 canvas->drawBitmapRect(fCircleBM, SkRect::MakeXYWH(20,20,60,60), &blackPaint);
     58             canvas->restore();
     59         canvas->restore();
     60     }
     61 
     62  private:
     63     const SkImageInfo fImageInfo;
     64     SkBitmap fCircleBM;
     65 };
     66 
     67 class RecordingStrategy {
     68  public:
     69     virtual ~RecordingStrategy() {}
     70     virtual const SkBitmap& recordAndReplay(const Drawer& drawer,
     71                                             const SkRect& intoClip,
     72                                             SkBlendMode) = 0;
     73 };
     74 
     75 class BitmapBackedCanvasStrategy : public RecordingStrategy {
     76     // This version just draws into a bitmap-backed canvas.
     77  public:
     78     BitmapBackedCanvasStrategy(const SkImageInfo& imageInfo) {
     79         fBitmap.allocPixels(imageInfo);
     80     }
     81 
     82     const SkBitmap& recordAndReplay(const Drawer& drawer, const SkRect& intoClip,
     83                                     SkBlendMode mode) override {
     84         SkCanvas canvas(fBitmap);
     85         canvas.clear(0xffffffff);
     86         // Note that the scene is drawn just into the clipped region!
     87         canvas.clipRect(intoClip);
     88         drawer.draw(&canvas, intoClip, mode); // Shouild be canvas-wide...
     89         return fBitmap;
     90     }
     91 
     92  private:
     93     SkBitmap fBitmap;
     94 };
     95 
     96 class PictureStrategy : public RecordingStrategy {
     97     // This version draws the entire scene into an SkPictureRecorder.
     98     // Then it then replays the scene through a clip rectangle.
     99     // This backend proved to be buggy.
    100  public:
    101     PictureStrategy(const SkImageInfo& imageInfo) {
    102         fBitmap.allocPixels(imageInfo);
    103         fWidth  = imageInfo.width();
    104         fHeight = imageInfo.height();
    105     }
    106 
    107     const SkBitmap& recordAndReplay(const Drawer& drawer, const SkRect& intoClip,
    108                                     SkBlendMode mode) override {
    109         SkRTreeFactory factory;
    110         SkPictureRecorder recorder;
    111         SkRect canvasRect(SkRect::MakeWH(SkIntToScalar(fWidth),SkIntToScalar(fHeight)));
    112         SkCanvas* canvas = recorder.beginRecording(SkIntToScalar(fWidth),
    113                                                    SkIntToScalar(fHeight),
    114                                                    &factory);
    115         drawer.draw(canvas, canvasRect, mode);
    116         sk_sp<SkPicture> picture(recorder.finishRecordingAsPicture());
    117 
    118         SkCanvas replayCanvas(fBitmap);
    119         replayCanvas.clear(0xffffffff);
    120         replayCanvas.clipRect(intoClip);
    121         picture->playback(&replayCanvas);
    122         return fBitmap;
    123     }
    124 
    125  private:
    126     SkBitmap fBitmap;
    127     int fWidth;
    128     int fHeight;
    129 };
    130 
    131 } // namespace
    132 
    133 
    134 DEF_TEST(SkRecordingAccuracyXfermode, reporter) {
    135 #define FINEGRAIN 0
    136     const Drawer drawer;
    137 
    138     BitmapBackedCanvasStrategy golden(drawer.imageInfo());
    139     PictureStrategy picture(drawer.imageInfo());
    140 
    141 #if !FINEGRAIN
    142     unsigned numErrors = 0;
    143     SkString errors;
    144 #endif
    145 
    146     for (int iMode = 0; iMode < int(SkBlendMode::kLastMode); iMode++) {
    147         const SkRect& clip = SkRect::MakeXYWH(100, 0, 100, 100);
    148         SkBlendMode mode = SkBlendMode(iMode);
    149 
    150         const SkBitmap& goldenBM = golden.recordAndReplay(drawer, clip, mode);
    151         const SkBitmap& pictureBM = picture.recordAndReplay(drawer, clip, mode);
    152 
    153         size_t pixelsSize = goldenBM.getSize();
    154         REPORTER_ASSERT(reporter, pixelsSize == pictureBM.getSize());
    155 
    156         // The pixel arrays should match.
    157 #if FINEGRAIN
    158         REPORTER_ASSERT(reporter,
    159                         0 == memcmp(goldenBM.getPixels(), pictureBM.getPixels(), pixelsSize));
    160 #else
    161         if (memcmp(goldenBM.getPixels(), pictureBM.getPixels(), pixelsSize)) {
    162             numErrors++;
    163             errors.appendf("For SkXfermode %d %s:    SkPictureRecorder bitmap is wrong\n",
    164                            iMode, SkBlendMode_Name(mode));
    165         }
    166 #endif
    167     }
    168 
    169 #if !FINEGRAIN
    170     REPORTER_ASSERT_MESSAGE(reporter, 0 == numErrors, errors.c_str());
    171 #endif
    172 }
    173