surface.cpp 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266
  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. #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/SkColorSpace.h"
  12. #include "include/core/SkFont.h"
  13. #include "include/core/SkImage.h"
  14. #include "include/core/SkImageInfo.h"
  15. #include "include/core/SkPaint.h"
  16. #include "include/core/SkPoint.h"
  17. #include "include/core/SkRect.h"
  18. #include "include/core/SkRefCnt.h"
  19. #include "include/core/SkScalar.h"
  20. #include "include/core/SkShader.h"
  21. #include "include/core/SkSize.h"
  22. #include "include/core/SkString.h"
  23. #include "include/core/SkSurface.h"
  24. #include "include/core/SkSurfaceProps.h"
  25. #include "include/core/SkTileMode.h"
  26. #include "include/core/SkTypeface.h"
  27. #include "include/core/SkTypes.h"
  28. #include "include/effects/SkGradientShader.h"
  29. #include "include/utils/SkTextUtils.h"
  30. #include "tools/ToolUtils.h"
  31. class GrContext;
  32. #define W 200
  33. #define H 100
  34. static sk_sp<SkShader> make_shader() {
  35. int a = 0x99;
  36. int b = 0xBB;
  37. SkPoint pts[] = { { 0, 0 }, { W, H } };
  38. SkColor colors[] = { SkColorSetRGB(a, a, a), SkColorSetRGB(b, b, b) };
  39. return SkGradientShader::MakeLinear(pts, colors, nullptr, 2, SkTileMode::kClamp);
  40. }
  41. static sk_sp<SkSurface> make_surface(GrContext* ctx, const SkImageInfo& info, SkPixelGeometry geo) {
  42. SkSurfaceProps props(0, geo);
  43. if (ctx) {
  44. return SkSurface::MakeRenderTarget(ctx, SkBudgeted::kNo, info, 0, &props);
  45. } else {
  46. return SkSurface::MakeRaster(info, &props);
  47. }
  48. }
  49. static void test_draw(SkCanvas* canvas, const char label[]) {
  50. SkPaint paint;
  51. paint.setAntiAlias(true);
  52. paint.setDither(true);
  53. paint.setShader(make_shader());
  54. canvas->drawRect(SkRect::MakeWH(W, H), paint);
  55. paint.setShader(nullptr);
  56. paint.setColor(SK_ColorWHITE);
  57. SkFont font(ToolUtils::create_portable_typeface(), 32);
  58. font.setEdging(SkFont::Edging::kSubpixelAntiAlias);
  59. SkTextUtils::DrawString(canvas, label, W / 2, H * 3 / 4, font, paint,
  60. SkTextUtils::kCenter_Align);
  61. }
  62. class SurfacePropsGM : public skiagm::GM {
  63. public:
  64. SurfacePropsGM() {}
  65. protected:
  66. SkString onShortName() override {
  67. return SkString("surfaceprops");
  68. }
  69. SkISize onISize() override {
  70. return SkISize::Make(W, H * 5);
  71. }
  72. void onDraw(SkCanvas* canvas) override {
  73. GrContext* ctx = canvas->getGrContext();
  74. // must be opaque to have a hope of testing LCD text
  75. const SkImageInfo info = SkImageInfo::MakeN32(W, H, kOpaque_SkAlphaType);
  76. const struct {
  77. SkPixelGeometry fGeo;
  78. const char* fLabel;
  79. } recs[] = {
  80. { kUnknown_SkPixelGeometry, "Unknown" },
  81. { kRGB_H_SkPixelGeometry, "RGB_H" },
  82. { kBGR_H_SkPixelGeometry, "BGR_H" },
  83. { kRGB_V_SkPixelGeometry, "RGB_V" },
  84. { kBGR_V_SkPixelGeometry, "BGR_V" },
  85. };
  86. SkScalar x = 0;
  87. SkScalar y = 0;
  88. for (const auto& rec : recs) {
  89. auto surface(make_surface(ctx, info, rec.fGeo));
  90. if (!surface) {
  91. SkDebugf("failed to create surface! label: %s", rec.fLabel);
  92. continue;
  93. }
  94. test_draw(surface->getCanvas(), rec.fLabel);
  95. surface->draw(canvas, x, y, nullptr);
  96. y += H;
  97. }
  98. }
  99. private:
  100. typedef GM INHERITED;
  101. };
  102. DEF_GM( return new SurfacePropsGM )
  103. #ifdef SK_DEBUG
  104. static bool equal(const SkSurfaceProps& a, const SkSurfaceProps& b) {
  105. return a.flags() == b.flags() && a.pixelGeometry() == b.pixelGeometry();
  106. }
  107. #endif
  108. class NewSurfaceGM : public skiagm::GM {
  109. public:
  110. NewSurfaceGM() {}
  111. protected:
  112. SkString onShortName() override {
  113. return SkString("surfacenew");
  114. }
  115. SkISize onISize() override {
  116. return SkISize::Make(300, 140);
  117. }
  118. static void drawInto(SkCanvas* canvas) {
  119. canvas->drawColor(SK_ColorRED);
  120. }
  121. void onDraw(SkCanvas* canvas) override {
  122. SkImageInfo info = SkImageInfo::MakeN32Premul(100, 100);
  123. auto surf(ToolUtils::makeSurface(canvas, info, nullptr));
  124. drawInto(surf->getCanvas());
  125. sk_sp<SkImage> image(surf->makeImageSnapshot());
  126. canvas->drawImage(image, 10, 10, nullptr);
  127. auto surf2(surf->makeSurface(info));
  128. drawInto(surf2->getCanvas());
  129. // Assert that the props were communicated transitively through the first image
  130. SkASSERT(equal(surf->props(), surf2->props()));
  131. sk_sp<SkImage> image2(surf2->makeImageSnapshot());
  132. canvas->drawImage(image2.get(), 10 + SkIntToScalar(image->width()) + 10, 10, nullptr);
  133. }
  134. private:
  135. typedef GM INHERITED;
  136. };
  137. DEF_GM( return new NewSurfaceGM )
  138. ///////////////////////////////////////////////////////////////////////////////////////////////////
  139. DEF_SIMPLE_GM(copy_on_write_retain, canvas, 256, 256) {
  140. const SkImageInfo info = SkImageInfo::MakeN32Premul(256, 256);
  141. sk_sp<SkSurface> surf = ToolUtils::makeSurface(canvas, info);
  142. surf->getCanvas()->clear(SK_ColorRED);
  143. // its important that image survives longer than the next draw, so the surface will see
  144. // an outstanding image, and have to decide if it should retain or discard those pixels
  145. sk_sp<SkImage> image = surf->makeImageSnapshot();
  146. // normally a clear+opaque should trigger the discard optimization, but since we have a clip
  147. // it should not (we need the previous red pixels).
  148. surf->getCanvas()->clipRect(SkRect::MakeWH(128, 256));
  149. surf->getCanvas()->clear(SK_ColorBLUE);
  150. // expect to see two rects: blue | red
  151. canvas->drawImage(surf->makeImageSnapshot(), 0, 0, nullptr);
  152. }
  153. DEF_SIMPLE_GM(copy_on_write_savelayer, canvas, 256, 256) {
  154. const SkImageInfo info = SkImageInfo::MakeN32Premul(256, 256);
  155. sk_sp<SkSurface> surf = ToolUtils::makeSurface(canvas, info);
  156. surf->getCanvas()->clear(SK_ColorRED);
  157. // its important that image survives longer than the next draw, so the surface will see
  158. // an outstanding image, and have to decide if it should retain or discard those pixels
  159. sk_sp<SkImage> image = surf->makeImageSnapshot();
  160. // now draw into a full-screen layer. This should (a) trigger a copy-on-write, but it should
  161. // not trigger discard, even tho its alpha (SK_ColorBLUE) is opaque, since it is in a layer
  162. // with a non-opaque paint.
  163. SkPaint paint;
  164. paint.setAlphaf(0.25f);
  165. surf->getCanvas()->saveLayer({0, 0, 256, 256}, &paint);
  166. surf->getCanvas()->clear(SK_ColorBLUE);
  167. surf->getCanvas()->restore();
  168. // expect to see two rects: blue blended on red
  169. canvas->drawImage(surf->makeImageSnapshot(), 0, 0, nullptr);
  170. }
  171. DEF_SIMPLE_GM(surface_underdraw, canvas, 256, 256) {
  172. SkImageInfo info = SkImageInfo::MakeN32Premul(256, 256, nullptr);
  173. auto surf = ToolUtils::makeSurface(canvas, info);
  174. const SkIRect subset = SkIRect::MakeLTRB(180, 0, 256, 256);
  175. // noisy background
  176. {
  177. SkPoint pts[] = {{0, 0}, {40, 50}};
  178. SkColor colors[] = {SK_ColorRED, SK_ColorBLUE};
  179. auto sh = SkGradientShader::MakeLinear(pts, colors, nullptr, 2, SkTileMode::kRepeat);
  180. SkPaint paint;
  181. paint.setShader(sh);
  182. surf->getCanvas()->drawPaint(paint);
  183. }
  184. // save away the right-hand strip, then clear it
  185. sk_sp<SkImage> saveImg = surf->makeImageSnapshot(subset);
  186. {
  187. SkPaint paint;
  188. paint.setBlendMode(SkBlendMode::kClear);
  189. surf->getCanvas()->drawRect(SkRect::Make(subset), paint);
  190. }
  191. // draw the "foreground"
  192. {
  193. SkPaint paint;
  194. paint.setColor(SK_ColorGREEN);
  195. SkRect r = { 0, 10, 256, 35 };
  196. while (r.fBottom < 256) {
  197. surf->getCanvas()->drawRect(r, paint);
  198. r.offset(0, r.height() * 2);
  199. }
  200. }
  201. // apply the "fade"
  202. {
  203. SkPoint pts[] = {{SkIntToScalar(subset.left()), 0}, {SkIntToScalar(subset.right()), 0}};
  204. SkColor colors[] = {0xFF000000, 0};
  205. auto sh = SkGradientShader::MakeLinear(pts, colors, nullptr, 2, SkTileMode::kClamp);
  206. SkPaint paint;
  207. paint.setShader(sh);
  208. paint.setBlendMode(SkBlendMode::kDstIn);
  209. surf->getCanvas()->drawRect(SkRect::Make(subset), paint);
  210. }
  211. // restore the original strip, drawing it "under" the current foreground
  212. {
  213. SkPaint paint;
  214. paint.setBlendMode(SkBlendMode::kDstOver);
  215. surf->getCanvas()->drawImage(saveImg,
  216. SkIntToScalar(subset.left()), SkIntToScalar(subset.top()),
  217. &paint);
  218. }
  219. // show it on screen
  220. surf->draw(canvas, 0, 0, nullptr);
  221. }