imagemakewithfilter.cpp 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432
  1. /*
  2. * Copyright 2016 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/SkColorFilter.h"
  12. #include "include/core/SkImage.h"
  13. #include "include/core/SkImageFilter.h"
  14. #include "include/core/SkImageInfo.h"
  15. #include "include/core/SkPaint.h"
  16. #include "include/core/SkPoint.h"
  17. #include "include/core/SkPoint3.h"
  18. #include "include/core/SkRect.h"
  19. #include "include/core/SkRefCnt.h"
  20. #include "include/core/SkRegion.h"
  21. #include "include/core/SkScalar.h"
  22. #include "include/core/SkSize.h"
  23. #include "include/core/SkString.h"
  24. #include "include/core/SkSurface.h"
  25. #include "include/core/SkTypes.h"
  26. #include "include/effects/SkAlphaThresholdFilter.h"
  27. #include "include/effects/SkArithmeticImageFilter.h"
  28. #include "include/effects/SkBlurImageFilter.h"
  29. #include "include/effects/SkColorFilterImageFilter.h"
  30. #include "include/effects/SkDisplacementMapEffect.h"
  31. #include "include/effects/SkDropShadowImageFilter.h"
  32. #include "include/effects/SkImageSource.h"
  33. #include "include/effects/SkLightingImageFilter.h"
  34. #include "include/effects/SkMatrixConvolutionImageFilter.h"
  35. #include "include/effects/SkMergeImageFilter.h"
  36. #include "include/effects/SkMorphologyImageFilter.h"
  37. #include "include/effects/SkOffsetImageFilter.h"
  38. #include "include/effects/SkTileImageFilter.h"
  39. #include "include/effects/SkXfermodeImageFilter.h"
  40. #include "include/gpu/GrContext.h"
  41. #include "tools/Resources.h"
  42. #include "tools/ToolUtils.h"
  43. #include <utility>
  44. ///////////////////////////////////////////////////////////////////////////////
  45. static void show_bounds(SkCanvas* canvas, const SkIRect* clip, const SkIRect* inSubset,
  46. const SkIRect* outSubset) {
  47. const SkIRect* rects[] { clip, inSubset, outSubset };
  48. SkColor colors[] { SK_ColorBLUE, SK_ColorYELLOW, SK_ColorRED };
  49. SkPaint paint;
  50. paint.setStyle(SkPaint::kStroke_Style);
  51. for (size_t i = 0; i < SK_ARRAY_COUNT(rects); ++i) {
  52. // Skip null bounds rects, since not all methods have subsets
  53. if (rects[i]) {
  54. paint.setColor(colors[i]);
  55. canvas->drawRect(SkRect::Make(*(rects[i])), paint);
  56. }
  57. }
  58. }
  59. // Factories for creating image filters, either with or without a cropRect
  60. // (this could go away if there was a SkImageFilter::makeWithCropRect() function, but that seems
  61. // less generally useful).
  62. typedef sk_sp<SkImageFilter> (*FilterFactory)(sk_sp<SkImage> auxImage, const SkIRect* cropRect);
  63. SkImageFilter::CropRect make_crop(const SkIRect* cropRect) {
  64. if (cropRect) {
  65. return SkImageFilter::CropRect(SkRect::Make(*cropRect));
  66. } else {
  67. return SkImageFilter::CropRect(SkRect(), 0x0 /* no edge flags */);
  68. }
  69. }
  70. static sk_sp<SkImageFilter> color_filter_factory(sk_sp<SkImage> auxImage, const SkIRect* cropRect) {
  71. // The color filter uses kSrcIn so that it respects the transparency introduced by clamping;
  72. // using kSrc would just turn the entire out rect to green regardless.
  73. auto cf = SkColorFilters::Blend(SK_ColorGREEN, SkBlendMode::kSrcIn);
  74. auto crop = make_crop(cropRect);
  75. return SkColorFilterImageFilter::Make(std::move(cf), nullptr, &crop);
  76. }
  77. static sk_sp<SkImageFilter> blur_filter_factory(sk_sp<SkImage> auxImage, const SkIRect* cropRect) {
  78. auto crop = make_crop(cropRect);
  79. return SkBlurImageFilter::Make(2.0f, 2.0f, nullptr, &crop);
  80. }
  81. static sk_sp<SkImageFilter> drop_shadow_factory(sk_sp<SkImage> auxImage, const SkIRect* cropRect) {
  82. auto crop = make_crop(cropRect);
  83. return SkDropShadowImageFilter::Make(
  84. 10.0f, 5.0f, 3.0f, 3.0f, SK_ColorBLUE,
  85. SkDropShadowImageFilter::kDrawShadowAndForeground_ShadowMode,
  86. nullptr, &crop);
  87. }
  88. static sk_sp<SkImageFilter> offset_factory(sk_sp<SkImage> auxImage, const SkIRect* cropRect) {
  89. auto crop = make_crop(cropRect);
  90. return SkOffsetImageFilter::Make(10.f, 5.f, nullptr, &crop);
  91. }
  92. static sk_sp<SkImageFilter> dilate_factory(sk_sp<SkImage> auxImage, const SkIRect* cropRect) {
  93. auto crop = make_crop(cropRect);
  94. return SkDilateImageFilter::Make(10.f, 5.f, nullptr, &crop);
  95. }
  96. static sk_sp<SkImageFilter> erode_factory(sk_sp<SkImage> auxImage, const SkIRect* cropRect) {
  97. auto crop = make_crop(cropRect);
  98. return SkErodeImageFilter::Make(10.f, 5.f, nullptr, &crop);
  99. }
  100. static sk_sp<SkImageFilter> displacement_factory(sk_sp<SkImage> auxImage, const SkIRect* cropRect) {
  101. auto crop = make_crop(cropRect);
  102. sk_sp<SkImageFilter> displacement = SkImageSource::Make(std::move(auxImage));
  103. return SkDisplacementMapEffect::Make(
  104. SkDisplacementMapEffect::kR_ChannelSelectorType,
  105. SkDisplacementMapEffect::kG_ChannelSelectorType,
  106. 40.f, std::move(displacement), nullptr, &crop);
  107. }
  108. static sk_sp<SkImageFilter> arithmetic_factory(sk_sp<SkImage> auxImage, const SkIRect* cropRect) {
  109. auto crop = make_crop(cropRect);
  110. sk_sp<SkImageFilter> background = SkImageSource::Make(std::move(auxImage));
  111. return SkArithmeticImageFilter::Make(0.0f, .6f, 1.f, 0.f, false, std::move(background),
  112. nullptr, &crop);
  113. }
  114. static sk_sp<SkImageFilter> xfermode_factory(sk_sp<SkImage> auxImage, const SkIRect* cropRect) {
  115. auto crop = make_crop(cropRect);
  116. sk_sp<SkImageFilter> background = SkImageSource::Make(std::move(auxImage));
  117. return SkXfermodeImageFilter::Make(
  118. SkBlendMode::kModulate, std::move(background), nullptr, &crop);
  119. }
  120. static sk_sp<SkImageFilter> convolution_factory(sk_sp<SkImage> auxImage, const SkIRect* cropRect) {
  121. auto crop = make_crop(cropRect);
  122. SkISize kernelSize = SkISize::Make(3, 3);
  123. SkIPoint kernelOffset = SkIPoint::Make(1, 1);
  124. // A Laplacian edge detector, ee https://en.wikipedia.org/wiki/Kernel_(image_processing)
  125. SkScalar kernel[9] = {-1.f, -1.f, -1.f,
  126. -1.f, 8.f, -1.f,
  127. -1.f, -1.f, -1.f};
  128. return SkMatrixConvolutionImageFilter::Make(
  129. kernelSize, kernel, 1.f, 0.f, kernelOffset,
  130. SkMatrixConvolutionImageFilter::kClamp_TileMode, false, nullptr, &crop);
  131. }
  132. static sk_sp<SkImageFilter> matrix_factory(sk_sp<SkImage> auxImage, const SkIRect* cropRect) {
  133. SkMatrix matrix = SkMatrix::I();
  134. matrix.setRotate(45.f, 50.f, 50.f);
  135. // This doesn't support a cropRect
  136. return SkImageFilter::MakeMatrixFilter(matrix, kLow_SkFilterQuality, nullptr);
  137. }
  138. static sk_sp<SkImageFilter> alpha_threshold_factory(sk_sp<SkImage> auxImage,
  139. const SkIRect* cropRect) {
  140. auto crop = make_crop(cropRect);
  141. // Centered cross with higher opacity
  142. SkRegion region(SkIRect::MakeLTRB(30, 45, 70, 55));
  143. region.op(SkIRect::MakeLTRB(45, 30, 55, 70), SkRegion::kUnion_Op);
  144. return SkAlphaThresholdFilter::Make(region, 1.f, .2f, nullptr, &crop);
  145. }
  146. static sk_sp<SkImageFilter> lighting_factory(sk_sp<SkImage> auxImage, const SkIRect* cropRect) {
  147. auto crop = make_crop(cropRect);
  148. // Must convert the RGB values of the source to alpha, since that is what the lighting filters
  149. // use to estimate their normals. This color matrix changes the color to white and the alpha
  150. // to be equal to the approx. luminance of the original color.
  151. static const float kMatrix[20] = {
  152. 0.f, 0.f, 0.f, 0.f, 1.f,
  153. 0.f, 0.f, 0.f, 0.f, 1.f,
  154. 0.f, 0.f, 0.f, 0.f, 1.f,
  155. 0.2126f, 0.7152f, 0.0722f, 0.f, 0.f
  156. };
  157. sk_sp<SkImageFilter> srcToAlpha = SkColorFilterImageFilter::Make(
  158. SkColorFilters::Matrix(kMatrix), nullptr);
  159. // Combine both specular and diffuse into a single DAG since they use separate internal filter
  160. // implementations.
  161. SkScalar sinAzimuth = SkScalarSin(SkDegreesToRadians(225.f)),
  162. cosAzimuth = SkScalarCos(SkDegreesToRadians(225.f));
  163. SkPoint3 spotTarget = SkPoint3::Make(SkIntToScalar(40), SkIntToScalar(40), 0);
  164. SkPoint3 diffLocation = SkPoint3::Make(spotTarget.fX + 50 * cosAzimuth,
  165. spotTarget.fY + 50 * sinAzimuth,
  166. SkIntToScalar(10));
  167. SkPoint3 specLocation = SkPoint3::Make(spotTarget.fX - 50 * sinAzimuth,
  168. spotTarget.fY + 50 * cosAzimuth,
  169. SkIntToScalar(10));
  170. sk_sp<SkImageFilter> diffuse = SkLightingImageFilter::MakePointLitDiffuse(
  171. diffLocation, SK_ColorWHITE, /* scale */ 1.f, /* kd */ 2.f, srcToAlpha, &crop);
  172. sk_sp<SkImageFilter> specular = SkLightingImageFilter::MakePointLitSpecular(
  173. specLocation, SK_ColorRED, /* scale */ 1.f, /* ks */ 1.f, /* shine */ 8.f,
  174. srcToAlpha, &crop);
  175. return SkMergeImageFilter::Make(std::move(diffuse), std::move(specular), &crop);
  176. }
  177. static sk_sp<SkImageFilter> tile_factory(sk_sp<SkImage> auxImage, const SkIRect* cropRect) {
  178. // Tile the subset over a large region
  179. return SkTileImageFilter::Make(SkRect::MakeLTRB(25, 25, 75, 75), SkRect::MakeWH(100, 100),
  180. nullptr);
  181. }
  182. namespace {
  183. enum class Strategy {
  184. // Uses makeWithFilter, passing in subset and clip directly
  185. kMakeWithFilter,
  186. // Uses saveLayer after clipRect() to filter on the restore (i.e. reference image)
  187. kSaveLayer
  188. };
  189. };
  190. // In this GM, we're going to feed the inner portion of a 100x100 mandrill (i.e., strip off a
  191. // 25-wide border) through the makeWithFilter method. We'll then draw the appropriate subset of the
  192. // result to the screen at the given offset. Some filters rely on a secondary image, which will be a
  193. // 100x100 checkerboard. The original image is drawn in the background so that alignment is clear
  194. // when drawing the result at its reported offset.
  195. class ImageMakeWithFilterGM : public skiagm::GM {
  196. public:
  197. ImageMakeWithFilterGM (Strategy strategy, bool filterWithCropRect = false)
  198. : fStrategy(strategy)
  199. , fFilterWithCropRect(filterWithCropRect)
  200. , fMainImage(nullptr)
  201. , fAuxImage(nullptr) {}
  202. protected:
  203. SkString onShortName() override {
  204. SkString name = SkString("imagemakewithfilter");
  205. if (fFilterWithCropRect) {
  206. name.append("_crop");
  207. }
  208. if (fStrategy == Strategy::kSaveLayer) {
  209. name.append("_ref");
  210. }
  211. return name;
  212. }
  213. SkISize onISize() override { return SkISize::Make(1980, 860); }
  214. void onOnceBeforeDraw() override {
  215. SkImageInfo info = SkImageInfo::MakeN32(100, 100, kUnpremul_SkAlphaType);
  216. auto surface = SkSurface::MakeRaster(info, nullptr);
  217. sk_sp<SkImage> colorImage = GetResourceAsImage("images/mandrill_128.png");
  218. // Resize to 100x100
  219. surface->getCanvas()->drawImageRect(
  220. colorImage, SkRect::MakeWH(colorImage->width(), colorImage->height()),
  221. SkRect::MakeWH(info.width(), info.height()), nullptr);
  222. fMainImage = surface->makeImageSnapshot();
  223. ToolUtils::draw_checkerboard(surface->getCanvas());
  224. fAuxImage = surface->makeImageSnapshot();
  225. }
  226. void onDraw(SkCanvas* canvas) override {
  227. FilterFactory filters[] = {
  228. color_filter_factory,
  229. blur_filter_factory,
  230. drop_shadow_factory,
  231. offset_factory,
  232. dilate_factory,
  233. erode_factory,
  234. displacement_factory,
  235. arithmetic_factory,
  236. xfermode_factory,
  237. convolution_factory,
  238. matrix_factory,
  239. alpha_threshold_factory,
  240. lighting_factory,
  241. tile_factory
  242. };
  243. const char* filterNames[] = {
  244. "Color",
  245. "Blur",
  246. "Drop Shadow",
  247. "Offset",
  248. "Dilate",
  249. "Erode",
  250. "Displacement",
  251. "Arithmetic",
  252. "Xfer Mode",
  253. "Convolution",
  254. "Matrix Xform",
  255. "Alpha Threshold",
  256. "Lighting",
  257. "Tile"
  258. };
  259. static_assert(SK_ARRAY_COUNT(filters) == SK_ARRAY_COUNT(filterNames), "filter name length");
  260. SkIRect clipBounds[] {
  261. { -20, -20, 100, 100 },
  262. { 0, 0, 75, 75 },
  263. { 20, 20, 100, 100 },
  264. { -20, -20, 50, 50 },
  265. { 20, 20, 50, 50 },
  266. { 30, 30, 75, 75 }
  267. };
  268. // These need to be GPU-backed when on the GPU to ensure that the image filters use the GPU
  269. // code paths (otherwise they may choose to do CPU filtering then upload)
  270. sk_sp<SkImage> mainImage, auxImage;
  271. if (canvas->getGrContext()) {
  272. if (canvas->getGrContext()->abandoned()) {
  273. return;
  274. }
  275. mainImage = fMainImage->makeTextureImage(canvas->getGrContext(), nullptr);
  276. auxImage = fAuxImage->makeTextureImage(canvas->getGrContext(), nullptr);
  277. } else {
  278. mainImage = fMainImage;
  279. auxImage = fAuxImage;
  280. }
  281. SkASSERT(mainImage && (mainImage->isTextureBacked() || !canvas->getGrContext()));
  282. SkASSERT(auxImage && (auxImage->isTextureBacked() || !canvas->getGrContext()));
  283. SkScalar MARGIN = SkIntToScalar(40);
  284. SkScalar DX = mainImage->width() + MARGIN;
  285. SkScalar DY = auxImage->height() + MARGIN;
  286. // Header hinting at what the filters do
  287. SkPaint textPaint;
  288. textPaint.setAntiAlias(true);
  289. SkFont font(nullptr, 12);
  290. for (size_t i = 0; i < SK_ARRAY_COUNT(filterNames); ++i) {
  291. canvas->drawString(filterNames[i], DX * i + MARGIN, 15, font, textPaint);
  292. }
  293. canvas->translate(MARGIN, MARGIN);
  294. for (auto clipBound : clipBounds) {
  295. canvas->save();
  296. for (size_t i = 0; i < SK_ARRAY_COUNT(filters); ++i) {
  297. SkIRect subset = SkIRect::MakeXYWH(25, 25, 50, 50);
  298. SkIRect outSubset;
  299. // Draw the original image faintly so that it aids in checking alignment of the
  300. // filtered result.
  301. SkPaint alpha;
  302. alpha.setAlphaf(0.3f);
  303. canvas->drawImage(mainImage, 0, 0, &alpha);
  304. this->drawImageWithFilter(canvas, mainImage, auxImage, filters[i], clipBound,
  305. subset, &outSubset);
  306. // Draw outlines to highlight what was subset, what was cropped, and what was output
  307. // (no output subset is displayed for kSaveLayer since that information isn't avail)
  308. SkIRect* outSubsetBounds = nullptr;
  309. if (fStrategy != Strategy::kSaveLayer) {
  310. outSubsetBounds = &outSubset;
  311. }
  312. show_bounds(canvas, &clipBound, &subset, outSubsetBounds);
  313. canvas->translate(DX, 0);
  314. }
  315. canvas->restore();
  316. canvas->translate(0, DY);
  317. }
  318. }
  319. private:
  320. Strategy fStrategy;
  321. bool fFilterWithCropRect;
  322. sk_sp<SkImage> fMainImage;
  323. sk_sp<SkImage> fAuxImage;
  324. void drawImageWithFilter(SkCanvas* canvas, sk_sp<SkImage> mainImage, sk_sp<SkImage> auxImage,
  325. FilterFactory filterFactory, const SkIRect& clip,
  326. const SkIRect& subset, SkIRect* dstRect) {
  327. // When creating the filter with a crop rect equal to the clip, we should expect to see no
  328. // difference from a filter without a crop rect. However, if the CTM isn't managed properly
  329. // by makeWithFilter, then the final result will be the incorrect intersection of the clip
  330. // and the transformed crop rect.
  331. sk_sp<SkImageFilter> filter = filterFactory(auxImage,
  332. fFilterWithCropRect ? &clip : nullptr);
  333. if (fStrategy == Strategy::kSaveLayer) {
  334. SkAutoCanvasRestore acr(canvas, true);
  335. // Clip before the saveLayer with the filter
  336. canvas->clipRect(SkRect::Make(clip));
  337. // Put the image filter on the layer
  338. SkPaint paint;
  339. paint.setImageFilter(filter);
  340. canvas->saveLayer(nullptr, &paint);
  341. // Draw the original subset of the image
  342. canvas->drawImageRect(mainImage, subset, SkRect::Make(subset), nullptr);
  343. *dstRect = subset;
  344. } else {
  345. sk_sp<SkImage> result;
  346. SkIRect outSubset;
  347. SkIPoint offset;
  348. result = mainImage->makeWithFilter(filter.get(), subset, clip, &outSubset, &offset);
  349. SkASSERT(result);
  350. SkASSERT(mainImage->isTextureBacked() == result->isTextureBacked());
  351. *dstRect = SkIRect::MakeXYWH(offset.x(), offset.y(),
  352. outSubset.width(), outSubset.height());
  353. canvas->drawImageRect(result, outSubset, SkRect::Make(*dstRect), nullptr);
  354. }
  355. }
  356. typedef GM INHERITED;
  357. };
  358. // The different strategies should all look the same, with the exception of filters that affect
  359. // transparent black (i.e. the lighting filter). In the save layer case, the filter affects the
  360. // transparent pixels outside of the drawn subset, whereas the makeWithFilter is restricted. This
  361. // works as intended.
  362. DEF_GM( return new ImageMakeWithFilterGM(Strategy::kMakeWithFilter); )
  363. DEF_GM( return new ImageMakeWithFilterGM(Strategy::kSaveLayer); )
  364. // Test with crop rects on the image filters; should look identical to above if working correctly
  365. DEF_GM( return new ImageMakeWithFilterGM(Strategy::kMakeWithFilter, true); )
  366. DEF_GM( return new ImageMakeWithFilterGM(Strategy::kSaveLayer, true); )