aaclip.cpp 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292
  1. /*
  2. * Copyright 2011 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. #include "gm/gm.h"
  8. #include "include/core/SkBlendMode.h"
  9. #include "include/core/SkCanvas.h"
  10. #include "include/core/SkColor.h"
  11. #include "include/core/SkMatrix.h"
  12. #include "include/core/SkPaint.h"
  13. #include "include/core/SkPath.h"
  14. #include "include/core/SkRect.h"
  15. #include "include/core/SkScalar.h"
  16. #include "include/core/SkSize.h"
  17. #include "include/core/SkString.h"
  18. #include "src/core/SkCanvasPriv.h"
  19. #include "tools/ToolUtils.h"
  20. static void do_draw(SkCanvas* canvas, const SkRect& r) {
  21. SkPaint paint;
  22. paint.setBlendMode(SkBlendMode::kSrc);
  23. paint.setColor(0x800000FF);
  24. canvas->drawRect(r, paint);
  25. }
  26. /**
  27. * Exercise SkCanvasPriv::kDontClipToLayer_SaveLayerFlag flag, which does not limit the clip to the
  28. * layer's bounds. Thus when a draw occurs, it can (depending on "where" it is) draw into the layer
  29. * and/or draw onto the surrounding portions of the canvas, or both.
  30. *
  31. * This GM has a 100x100 rectangle (r), which its going to draw. However first it creates a layer
  32. * with this flag covering 1/2 of the rectangle (upper half). Then it draws the rect in SRC mode.
  33. *
  34. * The portion of the draw that intersects the layer should see the SRC draw, apply it to the layer
  35. * and then during restore, it will SRC_OVER that layer onto the canvas (SRC_OVER since the layer
  36. * has no paint, so it gets the default xfermode during restore).
  37. *
  38. * Note: when we talk about drawing directly into the "canvas", in fact we are drawing into an
  39. * "outer" layer we created (filled with red). This is a testing detail, so that our final
  40. * output image is itself opaque, otherwise we make it harder to view the GM as a PNG.
  41. *
  42. * The portion of the draw below the layer draws directly into the canvas. Since it is in SRC mode,
  43. * it will write 0x80 to the canvas' alpha, overwriting the "red", which then gets blended with
  44. * the GM's white background.
  45. *
  46. * The portion in the layer, will end up SRC_OVERing the 0x80 layer pixels onto the canvas' red
  47. * pixels, making magenta.
  48. *
  49. * Thus the expected result is the upper half to be magenta 0xFF7F0080, and the lower half to be
  50. * light blue 0xFF7F7FFF.
  51. */
  52. DEF_SIMPLE_GM(dont_clip_to_layer, canvas, 120, 120) {
  53. const SkRect r { 10, 10, 110, 110 };
  54. // Wrap the entire test inside a red layer, so we don't punch the actual gm's alpha with
  55. // kSrc_Mode, which makes it hard to view (we like our GMs to have opaque pixels).
  56. canvas->saveLayer(&r, nullptr);
  57. canvas->drawColor(SK_ColorRED);
  58. SkRect r0 = { 20, 20, 100, 55 };
  59. SkRect r1 = { 20, 65, 100, 100 };
  60. SkCanvas::SaveLayerRec rec;
  61. rec.fPaint = nullptr;
  62. rec.fBounds = &r0;
  63. rec.fBackdrop = nullptr;
  64. rec.fSaveLayerFlags = SkCanvasPriv::kDontClipToLayer_SaveLayerFlag;
  65. canvas->saveLayer(rec);
  66. rec.fBounds = &r1;
  67. canvas->saveLayer(rec);
  68. do_draw(canvas, r);
  69. canvas->restore();
  70. canvas->restore();
  71. canvas->restore(); // red-layer
  72. }
  73. /** Draw a 2px border around the target, then red behind the target;
  74. set the clip to match the target, then draw >> the target in blue.
  75. */
  76. static void draw(SkCanvas* canvas, SkRect& target, int x, int y) {
  77. SkPaint borderPaint;
  78. borderPaint.setColor(SkColorSetRGB(0x0, 0xDD, 0x0));
  79. borderPaint.setAntiAlias(true);
  80. SkPaint backgroundPaint;
  81. backgroundPaint.setColor(SkColorSetRGB(0xDD, 0x0, 0x0));
  82. backgroundPaint.setAntiAlias(true);
  83. SkPaint foregroundPaint;
  84. foregroundPaint.setColor(SkColorSetRGB(0x0, 0x0, 0xDD));
  85. foregroundPaint.setAntiAlias(true);
  86. canvas->save();
  87. canvas->translate(SkIntToScalar(x), SkIntToScalar(y));
  88. target.inset(SkIntToScalar(-2), SkIntToScalar(-2));
  89. canvas->drawRect(target, borderPaint);
  90. target.inset(SkIntToScalar(2), SkIntToScalar(2));
  91. canvas->drawRect(target, backgroundPaint);
  92. canvas->clipRect(target, true);
  93. target.inset(SkIntToScalar(-4), SkIntToScalar(-4));
  94. canvas->drawRect(target, foregroundPaint);
  95. canvas->restore();
  96. }
  97. static void draw_square(SkCanvas* canvas, int x, int y) {
  98. SkRect target (SkRect::MakeWH(10 * SK_Scalar1, 10 * SK_Scalar1));
  99. draw(canvas, target, x, y);
  100. }
  101. static void draw_column(SkCanvas* canvas, int x, int y) {
  102. SkRect target (SkRect::MakeWH(1 * SK_Scalar1, 10 * SK_Scalar1));
  103. draw(canvas, target, x, y);
  104. }
  105. static void draw_bar(SkCanvas* canvas, int x, int y) {
  106. SkRect target (SkRect::MakeWH(10 * SK_Scalar1, 1 * SK_Scalar1));
  107. draw(canvas, target, x, y);
  108. }
  109. static void draw_rect_tests(SkCanvas* canvas) {
  110. draw_square(canvas, 10, 10);
  111. draw_column(canvas, 30, 10);
  112. draw_bar(canvas, 10, 30);
  113. }
  114. /**
  115. Test a set of clipping problems discovered while writing blitAntiRect,
  116. and test all the code paths through the clipping blitters.
  117. Each region should show as a blue center surrounded by a 2px green
  118. border, with no red.
  119. */
  120. DEF_SIMPLE_GM(aaclip, canvas, 240, 120) {
  121. // Initial pixel-boundary-aligned draw
  122. draw_rect_tests(canvas);
  123. // Repeat 4x with .2, .4, .6, .8 px offsets
  124. canvas->translate(SK_Scalar1 / 5, SK_Scalar1 / 5);
  125. canvas->translate(SkIntToScalar(50), 0);
  126. draw_rect_tests(canvas);
  127. canvas->translate(SK_Scalar1 / 5, SK_Scalar1 / 5);
  128. canvas->translate(SkIntToScalar(50), 0);
  129. draw_rect_tests(canvas);
  130. canvas->translate(SK_Scalar1 / 5, SK_Scalar1 / 5);
  131. canvas->translate(SkIntToScalar(50), 0);
  132. draw_rect_tests(canvas);
  133. canvas->translate(SK_Scalar1 / 5, SK_Scalar1 / 5);
  134. canvas->translate(SkIntToScalar(50), 0);
  135. draw_rect_tests(canvas);
  136. }
  137. /////////////////////////////////////////////////////////////////////////
  138. #ifdef SK_BUILD_FOR_MAC
  139. #include "include/utils/mac/SkCGUtils.h"
  140. #include "src/core/SkMakeUnique.h"
  141. static std::unique_ptr<SkCanvas> make_canvas(const SkBitmap& bm) {
  142. const SkImageInfo& info = bm.info();
  143. if (info.bytesPerPixel() == 4) {
  144. return SkCanvas::MakeRasterDirectN32(info.width(), info.height(),
  145. (SkPMColor*)bm.getPixels(),
  146. bm.rowBytes());
  147. } else {
  148. return skstd::make_unique<SkCanvas>(bm);
  149. }
  150. }
  151. static void test_image(SkCanvas* canvas, const SkImageInfo& info) {
  152. SkBitmap bm;
  153. bm.allocPixels(info);
  154. if (info.isOpaque()) {
  155. bm.eraseColor(SK_ColorGREEN);
  156. } else {
  157. bm.eraseColor(0);
  158. }
  159. SkPaint paint;
  160. paint.setAntiAlias(true);
  161. paint.setColor(SK_ColorBLUE);
  162. make_canvas(bm)->drawCircle(50, 50, 49, paint);
  163. canvas->drawBitmap(bm, 10, 10);
  164. CGImageRef image = SkCreateCGImageRefWithColorspace(bm, nullptr);
  165. SkBitmap bm2;
  166. SkCreateBitmapFromCGImage(&bm2, image);
  167. canvas->drawBitmap(bm2, 10, 120);
  168. canvas->drawImage(SkMakeImageFromCGImage(image), 10, 120 + bm2.height() + 10);
  169. CGImageRelease(image);
  170. }
  171. DEF_SIMPLE_GM(cgimage, canvas, 800, 250) {
  172. const struct {
  173. SkColorType fCT;
  174. SkAlphaType fAT;
  175. } rec[] = {
  176. { kRGB_565_SkColorType, kOpaque_SkAlphaType },
  177. { kRGBA_8888_SkColorType, kPremul_SkAlphaType },
  178. { kRGBA_8888_SkColorType, kUnpremul_SkAlphaType },
  179. { kRGBA_8888_SkColorType, kOpaque_SkAlphaType },
  180. { kBGRA_8888_SkColorType, kPremul_SkAlphaType },
  181. { kBGRA_8888_SkColorType, kUnpremul_SkAlphaType },
  182. { kBGRA_8888_SkColorType, kOpaque_SkAlphaType },
  183. };
  184. for (size_t i = 0; i < SK_ARRAY_COUNT(rec); ++i) {
  185. SkImageInfo info = SkImageInfo::Make(100, 100, rec[i].fCT, rec[i].fAT);
  186. test_image(canvas, info);
  187. canvas->translate(info.width() + 10, 0);
  188. }
  189. }
  190. #endif
  191. ///////////////////////////////////////////////////////////////////////////////////////////////////
  192. // https://bug.skia.org/3716
  193. class ClipCubicGM : public skiagm::GM {
  194. const SkScalar W = 100;
  195. const SkScalar H = 240;
  196. SkPath fVPath, fHPath;
  197. public:
  198. ClipCubicGM() {
  199. fVPath.moveTo(W, 0);
  200. fVPath.cubicTo(W, H-10, 0, 10, 0, H);
  201. SkMatrix pivot;
  202. pivot.setRotate(90, W/2, H/2);
  203. fVPath.transform(pivot, &fHPath);
  204. }
  205. protected:
  206. SkString onShortName() override {
  207. return SkString("clipcubic");
  208. }
  209. SkISize onISize() override {
  210. return SkISize::Make(400, 410);
  211. }
  212. void doDraw(SkCanvas* canvas, const SkPath& path) {
  213. SkPaint paint;
  214. paint.setAntiAlias(true);
  215. paint.setColor(0xFFCCCCCC);
  216. canvas->drawPath(path, paint);
  217. paint.setColor(SK_ColorRED);
  218. paint.setStyle(SkPaint::kStroke_Style);
  219. canvas->drawPath(path, paint);
  220. }
  221. void drawAndClip(SkCanvas* canvas, const SkPath& path, SkScalar dx, SkScalar dy) {
  222. SkAutoCanvasRestore acr(canvas, true);
  223. SkRect r = SkRect::MakeXYWH(0, H/4, W, H/2);
  224. SkPaint paint;
  225. paint.setColor(ToolUtils::color_to_565(0xFF8888FF));
  226. canvas->drawRect(r, paint);
  227. this->doDraw(canvas, path);
  228. canvas->translate(dx, dy);
  229. canvas->drawRect(r, paint);
  230. canvas->clipRect(r);
  231. this->doDraw(canvas, path);
  232. }
  233. void onDraw(SkCanvas* canvas) override {
  234. canvas->translate(80, 10);
  235. this->drawAndClip(canvas, fVPath, 200, 0);
  236. canvas->translate(0, 200);
  237. this->drawAndClip(canvas, fHPath, 200, 0);
  238. }
  239. private:
  240. typedef skiagm::GM INHERITED;
  241. };
  242. DEF_GM(return new ClipCubicGM;)