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