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 | 898235c | 2016-04-06 07:38:23 -0700 | [diff] [blame] | 68 | const SkImageInfo info = SkImageInfo::MakeN32(W, H, kOpaque_SkAlphaType, |
brianosman | 52ede1d | 2016-06-20 08:25:02 -0700 | [diff] [blame] | 69 | sk_ref_sp(canvas->imageInfo().colorSpace())); |
reed | 4a8126e | 2014-09-22 07:29:03 -0700 | [diff] [blame] | 70 | |
| 71 | const struct { |
| 72 | SkPixelGeometry fGeo; |
| 73 | const char* fLabel; |
scroggo | e6cad6b | 2016-05-09 13:20:58 -0700 | [diff] [blame] | 74 | } recs[] = { |
reed | 4a8126e | 2014-09-22 07:29:03 -0700 | [diff] [blame] | 75 | { kUnknown_SkPixelGeometry, "Unknown" }, |
| 76 | { kRGB_H_SkPixelGeometry, "RGB_H" }, |
| 77 | { kBGR_H_SkPixelGeometry, "BGR_H" }, |
| 78 | { kRGB_V_SkPixelGeometry, "RGB_V" }, |
| 79 | { kBGR_V_SkPixelGeometry, "BGR_V" }, |
| 80 | }; |
halcanary | 9d524f2 | 2016-03-29 09:03:52 -0700 | [diff] [blame] | 81 | |
reed | 4a8126e | 2014-09-22 07:29:03 -0700 | [diff] [blame] | 82 | SkScalar x = 0; |
reed | 7c12354 | 2016-08-18 09:30:44 -0700 | [diff] [blame] | 83 | SkScalar y = 0; |
| 84 | for (const auto& rec : recs) { |
| 85 | auto surface(make_surface(ctx, info, rec.fGeo)); |
| 86 | if (!surface) { |
| 87 | SkDebugf("failed to create surface! label: %s", rec.fLabel); |
| 88 | continue; |
reed | 4a8126e | 2014-09-22 07:29:03 -0700 | [diff] [blame] | 89 | } |
reed | 7c12354 | 2016-08-18 09:30:44 -0700 | [diff] [blame] | 90 | test_draw(surface->getCanvas(), rec.fLabel); |
| 91 | surface->draw(canvas, x, y, nullptr); |
| 92 | y += H; |
reed | 4a8126e | 2014-09-22 07:29:03 -0700 | [diff] [blame] | 93 | } |
| 94 | } |
| 95 | |
| 96 | private: |
| 97 | typedef GM INHERITED; |
| 98 | }; |
reed | 4a8126e | 2014-09-22 07:29:03 -0700 | [diff] [blame] | 99 | DEF_GM( return new SurfacePropsGM ) |
reed | 4af267b | 2014-11-21 08:46:37 -0800 | [diff] [blame] | 100 | |
| 101 | #ifdef SK_DEBUG |
| 102 | static bool equal(const SkSurfaceProps& a, const SkSurfaceProps& b) { |
| 103 | return a.flags() == b.flags() && a.pixelGeometry() == b.pixelGeometry(); |
| 104 | } |
| 105 | #endif |
| 106 | |
| 107 | class NewSurfaceGM : public skiagm::GM { |
| 108 | public: |
| 109 | NewSurfaceGM() {} |
| 110 | |
| 111 | protected: |
mtklein | 36352bf | 2015-03-25 18:17:31 -0700 | [diff] [blame] | 112 | SkString onShortName() override { |
reed | 4af267b | 2014-11-21 08:46:37 -0800 | [diff] [blame] | 113 | return SkString("surfacenew"); |
| 114 | } |
| 115 | |
mtklein | 36352bf | 2015-03-25 18:17:31 -0700 | [diff] [blame] | 116 | SkISize onISize() override { |
reed | 4af267b | 2014-11-21 08:46:37 -0800 | [diff] [blame] | 117 | return SkISize::Make(300, 140); |
| 118 | } |
| 119 | |
| 120 | static void drawInto(SkCanvas* canvas) { |
| 121 | canvas->drawColor(SK_ColorRED); |
| 122 | } |
| 123 | |
mtklein | 36352bf | 2015-03-25 18:17:31 -0700 | [diff] [blame] | 124 | void onDraw(SkCanvas* canvas) override { |
reed | 4af267b | 2014-11-21 08:46:37 -0800 | [diff] [blame] | 125 | SkImageInfo info = SkImageInfo::MakeN32Premul(100, 100); |
| 126 | |
reed | e8f3062 | 2016-03-23 18:59:25 -0700 | [diff] [blame] | 127 | auto surf(canvas->makeSurface(info, nullptr)); |
| 128 | if (!surf) { |
| 129 | surf = SkSurface::MakeRaster(info); |
reed | 4af267b | 2014-11-21 08:46:37 -0800 | [diff] [blame] | 130 | } |
| 131 | drawInto(surf->getCanvas()); |
| 132 | |
reed | 9ce9d67 | 2016-03-17 10:51:11 -0700 | [diff] [blame] | 133 | sk_sp<SkImage> image(surf->makeImageSnapshot()); |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 134 | canvas->drawImage(image, 10, 10, nullptr); |
reed | 4af267b | 2014-11-21 08:46:37 -0800 | [diff] [blame] | 135 | |
reed | e8f3062 | 2016-03-23 18:59:25 -0700 | [diff] [blame] | 136 | auto surf2(surf->makeSurface(info)); |
reed | 4af267b | 2014-11-21 08:46:37 -0800 | [diff] [blame] | 137 | drawInto(surf2->getCanvas()); |
| 138 | |
| 139 | // Assert that the props were communicated transitively through the first image |
| 140 | SkASSERT(equal(surf->props(), surf2->props())); |
| 141 | |
reed | 9ce9d67 | 2016-03-17 10:51:11 -0700 | [diff] [blame] | 142 | sk_sp<SkImage> image2(surf2->makeImageSnapshot()); |
| 143 | canvas->drawImage(image2.get(), 10 + SkIntToScalar(image->width()) + 10, 10, nullptr); |
reed | 4af267b | 2014-11-21 08:46:37 -0800 | [diff] [blame] | 144 | } |
| 145 | |
| 146 | private: |
| 147 | typedef GM INHERITED; |
| 148 | }; |
| 149 | DEF_GM( return new NewSurfaceGM ) |