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 | 7c12354 | 2016-08-18 09:30:44 -0700 | [diff] [blame] | 24 | static sk_sp<SkSurface> make_surface(GrContext* ctx, const SkImageInfo& info, SkPixelGeometry geo) { |
| 25 | SkSurfaceProps props(0, geo); |
reed | 4a8126e | 2014-09-22 07:29:03 -0700 | [diff] [blame] | 26 | if (ctx) { |
reed | e8f3062 | 2016-03-23 18:59:25 -0700 | [diff] [blame] | 27 | return SkSurface::MakeRenderTarget(ctx, SkBudgeted::kNo, info, 0, &props); |
reed | 4a8126e | 2014-09-22 07:29:03 -0700 | [diff] [blame] | 28 | } else { |
reed | e8f3062 | 2016-03-23 18:59:25 -0700 | [diff] [blame] | 29 | return SkSurface::MakeRaster(info, &props); |
reed | 4a8126e | 2014-09-22 07:29:03 -0700 | [diff] [blame] | 30 | } |
| 31 | } |
| 32 | |
| 33 | static void test_draw(SkCanvas* canvas, const char label[]) { |
| 34 | SkPaint paint; |
| 35 | |
| 36 | paint.setAntiAlias(true); |
| 37 | paint.setLCDRenderText(true); |
| 38 | paint.setDither(true); |
| 39 | |
reed | 1a9b964 | 2016-03-13 14:13:58 -0700 | [diff] [blame] | 40 | paint.setShader(make_shader()); |
reed | 4a8126e | 2014-09-22 07:29:03 -0700 | [diff] [blame] | 41 | canvas->drawRect(SkRect::MakeWH(W, H), paint); |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 42 | paint.setShader(nullptr); |
reed | 4a8126e | 2014-09-22 07:29:03 -0700 | [diff] [blame] | 43 | |
| 44 | paint.setColor(SK_ColorWHITE); |
| 45 | paint.setTextSize(32); |
| 46 | paint.setTextAlign(SkPaint::kCenter_Align); |
caryclark | f597c42 | 2015-07-28 10:37:53 -0700 | [diff] [blame] | 47 | sk_tool_utils::set_portable_typeface(&paint); |
reed | 4a8126e | 2014-09-22 07:29:03 -0700 | [diff] [blame] | 48 | canvas->drawText(label, strlen(label), W / 2, H * 3 / 4, paint); |
| 49 | } |
| 50 | |
| 51 | class SurfacePropsGM : public skiagm::GM { |
| 52 | public: |
| 53 | SurfacePropsGM() {} |
| 54 | |
| 55 | protected: |
mtklein | 36352bf | 2015-03-25 18:17:31 -0700 | [diff] [blame] | 56 | SkString onShortName() override { |
reed | 4a8126e | 2014-09-22 07:29:03 -0700 | [diff] [blame] | 57 | return SkString("surfaceprops"); |
| 58 | } |
| 59 | |
mtklein | 36352bf | 2015-03-25 18:17:31 -0700 | [diff] [blame] | 60 | SkISize onISize() override { |
reed | 7c12354 | 2016-08-18 09:30:44 -0700 | [diff] [blame] | 61 | return SkISize::Make(W, H * 5); |
reed | 4a8126e | 2014-09-22 07:29:03 -0700 | [diff] [blame] | 62 | } |
| 63 | |
mtklein | 36352bf | 2015-03-25 18:17:31 -0700 | [diff] [blame] | 64 | void onDraw(SkCanvas* canvas) override { |
reed | 4a8126e | 2014-09-22 07:29:03 -0700 | [diff] [blame] | 65 | GrContext* ctx = canvas->getGrContext(); |
| 66 | |
| 67 | // must be opaque to have a hope of testing LCD text |
brianosman | 0e22eb8 | 2016-08-30 07:07:59 -0700 | [diff] [blame] | 68 | const SkImageInfo info = SkImageInfo::MakeN32(W, H, kOpaque_SkAlphaType); |
reed | 4a8126e | 2014-09-22 07:29:03 -0700 | [diff] [blame] | 69 | |
| 70 | const struct { |
| 71 | SkPixelGeometry fGeo; |
| 72 | const char* fLabel; |
scroggo | e6cad6b | 2016-05-09 13:20:58 -0700 | [diff] [blame] | 73 | } recs[] = { |
reed | 4a8126e | 2014-09-22 07:29:03 -0700 | [diff] [blame] | 74 | { kUnknown_SkPixelGeometry, "Unknown" }, |
| 75 | { kRGB_H_SkPixelGeometry, "RGB_H" }, |
| 76 | { kBGR_H_SkPixelGeometry, "BGR_H" }, |
| 77 | { kRGB_V_SkPixelGeometry, "RGB_V" }, |
| 78 | { kBGR_V_SkPixelGeometry, "BGR_V" }, |
| 79 | }; |
halcanary | 9d524f2 | 2016-03-29 09:03:52 -0700 | [diff] [blame] | 80 | |
reed | 4a8126e | 2014-09-22 07:29:03 -0700 | [diff] [blame] | 81 | SkScalar x = 0; |
reed | 7c12354 | 2016-08-18 09:30:44 -0700 | [diff] [blame] | 82 | SkScalar y = 0; |
| 83 | for (const auto& rec : recs) { |
| 84 | auto surface(make_surface(ctx, info, rec.fGeo)); |
| 85 | if (!surface) { |
| 86 | SkDebugf("failed to create surface! label: %s", rec.fLabel); |
| 87 | continue; |
reed | 4a8126e | 2014-09-22 07:29:03 -0700 | [diff] [blame] | 88 | } |
reed | 7c12354 | 2016-08-18 09:30:44 -0700 | [diff] [blame] | 89 | test_draw(surface->getCanvas(), rec.fLabel); |
| 90 | surface->draw(canvas, x, y, nullptr); |
| 91 | y += H; |
reed | 4a8126e | 2014-09-22 07:29:03 -0700 | [diff] [blame] | 92 | } |
| 93 | } |
| 94 | |
| 95 | private: |
| 96 | typedef GM INHERITED; |
| 97 | }; |
reed | 4a8126e | 2014-09-22 07:29:03 -0700 | [diff] [blame] | 98 | DEF_GM( return new SurfacePropsGM ) |
reed | 4af267b | 2014-11-21 08:46:37 -0800 | [diff] [blame] | 99 | |
| 100 | #ifdef SK_DEBUG |
| 101 | static bool equal(const SkSurfaceProps& a, const SkSurfaceProps& b) { |
| 102 | return a.flags() == b.flags() && a.pixelGeometry() == b.pixelGeometry(); |
| 103 | } |
| 104 | #endif |
| 105 | |
| 106 | class NewSurfaceGM : public skiagm::GM { |
| 107 | public: |
| 108 | NewSurfaceGM() {} |
| 109 | |
| 110 | protected: |
mtklein | 36352bf | 2015-03-25 18:17:31 -0700 | [diff] [blame] | 111 | SkString onShortName() override { |
reed | 4af267b | 2014-11-21 08:46:37 -0800 | [diff] [blame] | 112 | return SkString("surfacenew"); |
| 113 | } |
| 114 | |
mtklein | 36352bf | 2015-03-25 18:17:31 -0700 | [diff] [blame] | 115 | SkISize onISize() override { |
reed | 4af267b | 2014-11-21 08:46:37 -0800 | [diff] [blame] | 116 | return SkISize::Make(300, 140); |
| 117 | } |
| 118 | |
| 119 | static void drawInto(SkCanvas* canvas) { |
| 120 | canvas->drawColor(SK_ColorRED); |
| 121 | } |
| 122 | |
mtklein | 36352bf | 2015-03-25 18:17:31 -0700 | [diff] [blame] | 123 | void onDraw(SkCanvas* canvas) override { |
reed | 4af267b | 2014-11-21 08:46:37 -0800 | [diff] [blame] | 124 | SkImageInfo info = SkImageInfo::MakeN32Premul(100, 100); |
| 125 | |
reed | e8f3062 | 2016-03-23 18:59:25 -0700 | [diff] [blame] | 126 | auto surf(canvas->makeSurface(info, nullptr)); |
| 127 | if (!surf) { |
| 128 | surf = SkSurface::MakeRaster(info); |
reed | 4af267b | 2014-11-21 08:46:37 -0800 | [diff] [blame] | 129 | } |
| 130 | drawInto(surf->getCanvas()); |
| 131 | |
reed | 9ce9d67 | 2016-03-17 10:51:11 -0700 | [diff] [blame] | 132 | sk_sp<SkImage> image(surf->makeImageSnapshot()); |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 133 | canvas->drawImage(image, 10, 10, nullptr); |
reed | 4af267b | 2014-11-21 08:46:37 -0800 | [diff] [blame] | 134 | |
reed | e8f3062 | 2016-03-23 18:59:25 -0700 | [diff] [blame] | 135 | auto surf2(surf->makeSurface(info)); |
reed | 4af267b | 2014-11-21 08:46:37 -0800 | [diff] [blame] | 136 | drawInto(surf2->getCanvas()); |
| 137 | |
| 138 | // Assert that the props were communicated transitively through the first image |
| 139 | SkASSERT(equal(surf->props(), surf2->props())); |
| 140 | |
reed | 9ce9d67 | 2016-03-17 10:51:11 -0700 | [diff] [blame] | 141 | sk_sp<SkImage> image2(surf2->makeImageSnapshot()); |
| 142 | canvas->drawImage(image2.get(), 10 + SkIntToScalar(image->width()) + 10, 10, nullptr); |
reed | 4af267b | 2014-11-21 08:46:37 -0800 | [diff] [blame] | 143 | } |
| 144 | |
| 145 | private: |
| 146 | typedef GM INHERITED; |
| 147 | }; |
| 148 | DEF_GM( return new NewSurfaceGM ) |
Mike Reed | d94abc5 | 2017-03-06 16:37:07 -0500 | [diff] [blame] | 149 | |
| 150 | /////////////////////////////////////////////////////////////////////////////////////////////////// |
| 151 | |
| 152 | DEF_SIMPLE_GM(copy_on_write_retain, canvas, 256, 256) { |
| 153 | const SkImageInfo info = SkImageInfo::MakeN32Premul(256, 256); |
| 154 | sk_sp<SkSurface> surf = canvas->makeSurface(info, nullptr); |
| 155 | if (!surf) { |
| 156 | surf = SkSurface::MakeRaster(info, nullptr); |
| 157 | } |
| 158 | |
| 159 | surf->getCanvas()->clear(SK_ColorRED); |
| 160 | // its important that image survives longer than the next draw, so the surface will see |
| 161 | // an outstanding image, and have to decide if it should retain or discard those pixels |
| 162 | sk_sp<SkImage> image = surf->makeImageSnapshot(); |
| 163 | |
| 164 | // normally a clear+opaque should trigger the discard optimization, but since we have a clip |
| 165 | // it should not (we need the previous red pixels). |
| 166 | surf->getCanvas()->clipRect(SkRect::MakeWH(128, 256)); |
| 167 | surf->getCanvas()->clear(SK_ColorBLUE); |
| 168 | |
| 169 | // expect to see two rects: blue | red |
| 170 | canvas->drawImage(surf->makeImageSnapshot(), 0, 0, nullptr); |
| 171 | } |
Mike Reed | a136136 | 2017-03-07 09:37:29 -0500 | [diff] [blame] | 172 | |
| 173 | DEF_SIMPLE_GM(copy_on_write_savelayer, canvas, 256, 256) { |
| 174 | const SkImageInfo info = SkImageInfo::MakeN32Premul(256, 256); |
| 175 | sk_sp<SkSurface> surf = canvas->makeSurface(info, nullptr); |
| 176 | if (!surf) { |
| 177 | surf = SkSurface::MakeRaster(info, nullptr); |
| 178 | } |
| 179 | |
| 180 | surf->getCanvas()->clear(SK_ColorRED); |
| 181 | // its important that image survives longer than the next draw, so the surface will see |
| 182 | // an outstanding image, and have to decide if it should retain or discard those pixels |
| 183 | sk_sp<SkImage> image = surf->makeImageSnapshot(); |
| 184 | |
| 185 | // now draw into a full-screen layer. This should (a) trigger a copy-on-write, but it should |
| 186 | // not trigger discard, even tho its alpha (SK_ColorBLUE) is opaque, since it is in a layer |
| 187 | // with a non-opaque paint. |
| 188 | SkPaint paint; |
| 189 | paint.setAlpha(0x40); |
| 190 | surf->getCanvas()->saveLayer({0, 0, 256, 256}, &paint); |
| 191 | surf->getCanvas()->clear(SK_ColorBLUE); |
| 192 | surf->getCanvas()->restore(); |
| 193 | |
| 194 | // expect to see two rects: blue blended on red |
| 195 | canvas->drawImage(surf->makeImageSnapshot(), 0, 0, nullptr); |
| 196 | } |