reed | 4a8126e | 2014-09-22 07:29:03 -0700 | [diff] [blame] | 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 "gm.h" |
| 9 | #include "SkGradientShader.h" |
| 10 | #include "SkSurface.h" |
| 11 | #include "SkSurfaceProps.h" |
| 12 | |
| 13 | #define W 200 |
| 14 | #define H 100 |
| 15 | |
reed | 1a9b964 | 2016-03-13 14:13:58 -0700 | [diff] [blame] | 16 | static sk_sp<SkShader> make_shader() { |
reed | 4a8126e | 2014-09-22 07:29:03 -0700 | [diff] [blame] | 17 | int a = 0x99; |
| 18 | int b = 0xBB; |
| 19 | SkPoint pts[] = { { 0, 0 }, { W, H } }; |
| 20 | SkColor colors[] = { SkColorSetRGB(a, a, a), SkColorSetRGB(b, b, b) }; |
reed | 1a9b964 | 2016-03-13 14:13:58 -0700 | [diff] [blame] | 21 | return SkGradientShader::MakeLinear(pts, colors, nullptr, 2, SkShader::kClamp_TileMode); |
reed | 4a8126e | 2014-09-22 07:29:03 -0700 | [diff] [blame] | 22 | } |
| 23 | |
reed | e8f3062 | 2016-03-23 18:59:25 -0700 | [diff] [blame] | 24 | static sk_sp<SkSurface> make_surface(GrContext* ctx, const SkImageInfo& info, SkPixelGeometry geo, |
brianosman | b461d34 | 2016-04-13 13:10:14 -0700 | [diff] [blame] | 25 | int disallowAA, int disallowDither, bool gammaCorrect) { |
reed | 4a8126e | 2014-09-22 07:29:03 -0700 | [diff] [blame] | 26 | uint32_t flags = 0; |
| 27 | if (disallowAA) { |
| 28 | flags |= SkSurfaceProps::kDisallowAntiAlias_Flag; |
| 29 | } |
| 30 | if (disallowDither) { |
| 31 | flags |= SkSurfaceProps::kDisallowDither_Flag; |
| 32 | } |
brianosman | b461d34 | 2016-04-13 13:10:14 -0700 | [diff] [blame] | 33 | if (gammaCorrect) { |
| 34 | flags |= SkSurfaceProps::kGammaCorrect_Flag; |
brianosman | 898235c | 2016-04-06 07:38:23 -0700 | [diff] [blame] | 35 | } |
reed | 4a8126e | 2014-09-22 07:29:03 -0700 | [diff] [blame] | 36 | |
| 37 | SkSurfaceProps props(flags, geo); |
| 38 | if (ctx) { |
reed | e8f3062 | 2016-03-23 18:59:25 -0700 | [diff] [blame] | 39 | return SkSurface::MakeRenderTarget(ctx, SkBudgeted::kNo, info, 0, &props); |
reed | 4a8126e | 2014-09-22 07:29:03 -0700 | [diff] [blame] | 40 | } else { |
reed | e8f3062 | 2016-03-23 18:59:25 -0700 | [diff] [blame] | 41 | return SkSurface::MakeRaster(info, &props); |
reed | 4a8126e | 2014-09-22 07:29:03 -0700 | [diff] [blame] | 42 | } |
| 43 | } |
| 44 | |
| 45 | static void test_draw(SkCanvas* canvas, const char label[]) { |
| 46 | SkPaint paint; |
| 47 | |
| 48 | paint.setAntiAlias(true); |
| 49 | paint.setLCDRenderText(true); |
| 50 | paint.setDither(true); |
| 51 | |
reed | 1a9b964 | 2016-03-13 14:13:58 -0700 | [diff] [blame] | 52 | paint.setShader(make_shader()); |
reed | 4a8126e | 2014-09-22 07:29:03 -0700 | [diff] [blame] | 53 | canvas->drawRect(SkRect::MakeWH(W, H), paint); |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 54 | paint.setShader(nullptr); |
reed | 4a8126e | 2014-09-22 07:29:03 -0700 | [diff] [blame] | 55 | |
| 56 | paint.setColor(SK_ColorWHITE); |
| 57 | paint.setTextSize(32); |
| 58 | paint.setTextAlign(SkPaint::kCenter_Align); |
caryclark | f597c42 | 2015-07-28 10:37:53 -0700 | [diff] [blame] | 59 | sk_tool_utils::set_portable_typeface(&paint); |
reed | 4a8126e | 2014-09-22 07:29:03 -0700 | [diff] [blame] | 60 | canvas->drawText(label, strlen(label), W / 2, H * 3 / 4, paint); |
| 61 | } |
| 62 | |
| 63 | class SurfacePropsGM : public skiagm::GM { |
| 64 | public: |
| 65 | SurfacePropsGM() {} |
| 66 | |
| 67 | protected: |
mtklein | 36352bf | 2015-03-25 18:17:31 -0700 | [diff] [blame] | 68 | SkString onShortName() override { |
reed | 4a8126e | 2014-09-22 07:29:03 -0700 | [diff] [blame] | 69 | return SkString("surfaceprops"); |
| 70 | } |
| 71 | |
mtklein | 36352bf | 2015-03-25 18:17:31 -0700 | [diff] [blame] | 72 | SkISize onISize() override { |
reed | 4a8126e | 2014-09-22 07:29:03 -0700 | [diff] [blame] | 73 | return SkISize::Make(W * 4, H * 5); |
| 74 | } |
| 75 | |
mtklein | 36352bf | 2015-03-25 18:17:31 -0700 | [diff] [blame] | 76 | void onDraw(SkCanvas* canvas) override { |
reed | 4a8126e | 2014-09-22 07:29:03 -0700 | [diff] [blame] | 77 | GrContext* ctx = canvas->getGrContext(); |
| 78 | |
| 79 | // must be opaque to have a hope of testing LCD text |
brianosman | 898235c | 2016-04-06 07:38:23 -0700 | [diff] [blame] | 80 | const SkImageInfo info = SkImageInfo::MakeN32(W, H, kOpaque_SkAlphaType, |
| 81 | canvas->imageInfo().profileType()); |
| 82 | SkSurfaceProps canvasProps(SkSurfaceProps::kLegacyFontHost_InitType); |
brianosman | 0e3c554 | 2016-04-13 13:56:21 -0700 | [diff] [blame] | 83 | bool gammaCorrect = canvas->getProps(&canvasProps) && canvasProps.isGammaCorrect(); |
reed | 4a8126e | 2014-09-22 07:29:03 -0700 | [diff] [blame] | 84 | |
| 85 | const struct { |
| 86 | SkPixelGeometry fGeo; |
| 87 | const char* fLabel; |
scroggo | e6cad6b | 2016-05-09 13:20:58 -0700 | [diff] [blame] | 88 | } recs[] = { |
reed | 4a8126e | 2014-09-22 07:29:03 -0700 | [diff] [blame] | 89 | { kUnknown_SkPixelGeometry, "Unknown" }, |
| 90 | { kRGB_H_SkPixelGeometry, "RGB_H" }, |
| 91 | { kBGR_H_SkPixelGeometry, "BGR_H" }, |
| 92 | { kRGB_V_SkPixelGeometry, "RGB_V" }, |
| 93 | { kBGR_V_SkPixelGeometry, "BGR_V" }, |
| 94 | }; |
halcanary | 9d524f2 | 2016-03-29 09:03:52 -0700 | [diff] [blame] | 95 | |
reed | 4a8126e | 2014-09-22 07:29:03 -0700 | [diff] [blame] | 96 | SkScalar x = 0; |
| 97 | for (int disallowAA = 0; disallowAA <= 1; ++disallowAA) { |
| 98 | for (int disallowDither = 0; disallowDither <= 1; ++disallowDither) { |
| 99 | SkScalar y = 0; |
scroggo | e6cad6b | 2016-05-09 13:20:58 -0700 | [diff] [blame] | 100 | for (const auto& rec : recs) { |
| 101 | auto surface(make_surface(ctx, info, rec.fGeo, disallowAA, disallowDither, |
brianosman | 0e3c554 | 2016-04-13 13:56:21 -0700 | [diff] [blame] | 102 | gammaCorrect)); |
scroggo | e6cad6b | 2016-05-09 13:20:58 -0700 | [diff] [blame] | 103 | if (!surface) { |
| 104 | SkDebugf("failed to create surface! label: %s AA: %s dither: %s\n", |
| 105 | rec.fLabel, (disallowAA == 1 ? "disallowed" : "allowed"), |
| 106 | (disallowDither == 1 ? "disallowed" : "allowed")); |
| 107 | continue; |
| 108 | } |
| 109 | test_draw(surface->getCanvas(), rec.fLabel); |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 110 | surface->draw(canvas, x, y, nullptr); |
reed | 4a8126e | 2014-09-22 07:29:03 -0700 | [diff] [blame] | 111 | y += H; |
| 112 | } |
| 113 | x += W; |
| 114 | } |
| 115 | } |
| 116 | } |
| 117 | |
| 118 | private: |
| 119 | typedef GM INHERITED; |
| 120 | }; |
reed | 4a8126e | 2014-09-22 07:29:03 -0700 | [diff] [blame] | 121 | DEF_GM( return new SurfacePropsGM ) |
reed | 4af267b | 2014-11-21 08:46:37 -0800 | [diff] [blame] | 122 | |
| 123 | #ifdef SK_DEBUG |
| 124 | static bool equal(const SkSurfaceProps& a, const SkSurfaceProps& b) { |
| 125 | return a.flags() == b.flags() && a.pixelGeometry() == b.pixelGeometry(); |
| 126 | } |
| 127 | #endif |
| 128 | |
| 129 | class NewSurfaceGM : public skiagm::GM { |
| 130 | public: |
| 131 | NewSurfaceGM() {} |
| 132 | |
| 133 | protected: |
mtklein | 36352bf | 2015-03-25 18:17:31 -0700 | [diff] [blame] | 134 | SkString onShortName() override { |
reed | 4af267b | 2014-11-21 08:46:37 -0800 | [diff] [blame] | 135 | return SkString("surfacenew"); |
| 136 | } |
| 137 | |
mtklein | 36352bf | 2015-03-25 18:17:31 -0700 | [diff] [blame] | 138 | SkISize onISize() override { |
reed | 4af267b | 2014-11-21 08:46:37 -0800 | [diff] [blame] | 139 | return SkISize::Make(300, 140); |
| 140 | } |
| 141 | |
| 142 | static void drawInto(SkCanvas* canvas) { |
| 143 | canvas->drawColor(SK_ColorRED); |
| 144 | } |
| 145 | |
mtklein | 36352bf | 2015-03-25 18:17:31 -0700 | [diff] [blame] | 146 | void onDraw(SkCanvas* canvas) override { |
reed | 4af267b | 2014-11-21 08:46:37 -0800 | [diff] [blame] | 147 | SkImageInfo info = SkImageInfo::MakeN32Premul(100, 100); |
| 148 | |
reed | e8f3062 | 2016-03-23 18:59:25 -0700 | [diff] [blame] | 149 | auto surf(canvas->makeSurface(info, nullptr)); |
| 150 | if (!surf) { |
| 151 | surf = SkSurface::MakeRaster(info); |
reed | 4af267b | 2014-11-21 08:46:37 -0800 | [diff] [blame] | 152 | } |
| 153 | drawInto(surf->getCanvas()); |
| 154 | |
reed | 9ce9d67 | 2016-03-17 10:51:11 -0700 | [diff] [blame] | 155 | sk_sp<SkImage> image(surf->makeImageSnapshot()); |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 156 | canvas->drawImage(image, 10, 10, nullptr); |
reed | 4af267b | 2014-11-21 08:46:37 -0800 | [diff] [blame] | 157 | |
reed | e8f3062 | 2016-03-23 18:59:25 -0700 | [diff] [blame] | 158 | auto surf2(surf->makeSurface(info)); |
reed | 4af267b | 2014-11-21 08:46:37 -0800 | [diff] [blame] | 159 | drawInto(surf2->getCanvas()); |
| 160 | |
| 161 | // Assert that the props were communicated transitively through the first image |
| 162 | SkASSERT(equal(surf->props(), surf2->props())); |
| 163 | |
reed | 9ce9d67 | 2016-03-17 10:51:11 -0700 | [diff] [blame] | 164 | sk_sp<SkImage> image2(surf2->makeImageSnapshot()); |
| 165 | canvas->drawImage(image2.get(), 10 + SkIntToScalar(image->width()) + 10, 10, nullptr); |
reed | 4af267b | 2014-11-21 08:46:37 -0800 | [diff] [blame] | 166 | } |
| 167 | |
| 168 | private: |
| 169 | typedef GM INHERITED; |
| 170 | }; |
| 171 | DEF_GM( return new NewSurfaceGM ) |