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 | |
| 16 | static SkShader* make_shader() { |
| 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) }; |
| 21 | return SkGradientShader::CreateLinear(pts, colors, NULL, 2, SkShader::kClamp_TileMode); |
| 22 | } |
| 23 | |
| 24 | static SkSurface* make_surface(GrContext* ctx, const SkImageInfo& info, SkPixelGeometry geo, |
| 25 | int disallowAA, int disallowDither) { |
| 26 | uint32_t flags = 0; |
| 27 | if (disallowAA) { |
| 28 | flags |= SkSurfaceProps::kDisallowAntiAlias_Flag; |
| 29 | } |
| 30 | if (disallowDither) { |
| 31 | flags |= SkSurfaceProps::kDisallowDither_Flag; |
| 32 | } |
| 33 | |
| 34 | SkSurfaceProps props(flags, geo); |
| 35 | if (ctx) { |
bsalomon | afe3005 | 2015-01-16 07:32:33 -0800 | [diff] [blame] | 36 | return SkSurface::NewRenderTarget(ctx, SkSurface::kNo_Budgeted, info, 0, &props); |
reed | 4a8126e | 2014-09-22 07:29:03 -0700 | [diff] [blame] | 37 | } else { |
| 38 | return SkSurface::NewRaster(info, &props); |
| 39 | } |
| 40 | } |
| 41 | |
| 42 | static void test_draw(SkCanvas* canvas, const char label[]) { |
| 43 | SkPaint paint; |
| 44 | |
| 45 | paint.setAntiAlias(true); |
| 46 | paint.setLCDRenderText(true); |
| 47 | paint.setDither(true); |
| 48 | |
| 49 | paint.setShader(make_shader())->unref(); |
| 50 | canvas->drawRect(SkRect::MakeWH(W, H), paint); |
| 51 | paint.setShader(NULL); |
| 52 | |
| 53 | paint.setColor(SK_ColorWHITE); |
| 54 | paint.setTextSize(32); |
| 55 | paint.setTextAlign(SkPaint::kCenter_Align); |
caryclark | f597c42 | 2015-07-28 10:37:53 -0700 | [diff] [blame] | 56 | sk_tool_utils::set_portable_typeface(&paint); |
reed | 4a8126e | 2014-09-22 07:29:03 -0700 | [diff] [blame] | 57 | canvas->drawText(label, strlen(label), W / 2, H * 3 / 4, paint); |
| 58 | } |
| 59 | |
| 60 | class SurfacePropsGM : public skiagm::GM { |
| 61 | public: |
| 62 | SurfacePropsGM() {} |
| 63 | |
| 64 | protected: |
mtklein | 36352bf | 2015-03-25 18:17:31 -0700 | [diff] [blame] | 65 | SkString onShortName() override { |
reed | 4a8126e | 2014-09-22 07:29:03 -0700 | [diff] [blame] | 66 | return SkString("surfaceprops"); |
| 67 | } |
| 68 | |
mtklein | 36352bf | 2015-03-25 18:17:31 -0700 | [diff] [blame] | 69 | SkISize onISize() override { |
reed | 4a8126e | 2014-09-22 07:29:03 -0700 | [diff] [blame] | 70 | return SkISize::Make(W * 4, H * 5); |
| 71 | } |
| 72 | |
mtklein | 36352bf | 2015-03-25 18:17:31 -0700 | [diff] [blame] | 73 | void onDraw(SkCanvas* canvas) override { |
reed | 4a8126e | 2014-09-22 07:29:03 -0700 | [diff] [blame] | 74 | GrContext* ctx = canvas->getGrContext(); |
| 75 | |
| 76 | // must be opaque to have a hope of testing LCD text |
| 77 | const SkImageInfo info = SkImageInfo::MakeN32(W, H, kOpaque_SkAlphaType); |
| 78 | |
| 79 | const struct { |
| 80 | SkPixelGeometry fGeo; |
| 81 | const char* fLabel; |
| 82 | } rec[] = { |
| 83 | { kUnknown_SkPixelGeometry, "Unknown" }, |
| 84 | { kRGB_H_SkPixelGeometry, "RGB_H" }, |
| 85 | { kBGR_H_SkPixelGeometry, "BGR_H" }, |
| 86 | { kRGB_V_SkPixelGeometry, "RGB_V" }, |
| 87 | { kBGR_V_SkPixelGeometry, "BGR_V" }, |
| 88 | }; |
| 89 | |
| 90 | SkScalar x = 0; |
| 91 | for (int disallowAA = 0; disallowAA <= 1; ++disallowAA) { |
| 92 | for (int disallowDither = 0; disallowDither <= 1; ++disallowDither) { |
| 93 | SkScalar y = 0; |
| 94 | for (size_t i = 0; i < SK_ARRAY_COUNT(rec); ++i) { |
| 95 | SkAutoTUnref<SkSurface> surface(make_surface(ctx, info, rec[i].fGeo, |
| 96 | disallowAA, disallowDither)); |
| 97 | test_draw(surface->getCanvas(), rec[i].fLabel); |
| 98 | surface->draw(canvas, x, y, NULL); |
| 99 | y += H; |
| 100 | } |
| 101 | x += W; |
| 102 | } |
| 103 | } |
| 104 | } |
| 105 | |
| 106 | private: |
| 107 | typedef GM INHERITED; |
| 108 | }; |
reed | 4a8126e | 2014-09-22 07:29:03 -0700 | [diff] [blame] | 109 | DEF_GM( return new SurfacePropsGM ) |
reed | 4af267b | 2014-11-21 08:46:37 -0800 | [diff] [blame] | 110 | |
| 111 | #ifdef SK_DEBUG |
| 112 | static bool equal(const SkSurfaceProps& a, const SkSurfaceProps& b) { |
| 113 | return a.flags() == b.flags() && a.pixelGeometry() == b.pixelGeometry(); |
| 114 | } |
| 115 | #endif |
| 116 | |
| 117 | class NewSurfaceGM : public skiagm::GM { |
| 118 | public: |
| 119 | NewSurfaceGM() {} |
| 120 | |
| 121 | protected: |
mtklein | 36352bf | 2015-03-25 18:17:31 -0700 | [diff] [blame] | 122 | SkString onShortName() override { |
reed | 4af267b | 2014-11-21 08:46:37 -0800 | [diff] [blame] | 123 | return SkString("surfacenew"); |
| 124 | } |
| 125 | |
mtklein | 36352bf | 2015-03-25 18:17:31 -0700 | [diff] [blame] | 126 | SkISize onISize() override { |
reed | 4af267b | 2014-11-21 08:46:37 -0800 | [diff] [blame] | 127 | return SkISize::Make(300, 140); |
| 128 | } |
| 129 | |
| 130 | static void drawInto(SkCanvas* canvas) { |
| 131 | canvas->drawColor(SK_ColorRED); |
| 132 | } |
| 133 | |
mtklein | 36352bf | 2015-03-25 18:17:31 -0700 | [diff] [blame] | 134 | void onDraw(SkCanvas* canvas) override { |
reed | 4af267b | 2014-11-21 08:46:37 -0800 | [diff] [blame] | 135 | SkImageInfo info = SkImageInfo::MakeN32Premul(100, 100); |
| 136 | |
| 137 | SkAutoTUnref<SkSurface> surf(canvas->newSurface(info, NULL)); |
| 138 | if (!surf.get()) { |
| 139 | surf.reset(SkSurface::NewRaster(info)); |
| 140 | } |
| 141 | drawInto(surf->getCanvas()); |
| 142 | |
| 143 | SkAutoTUnref<SkImage> image(surf->newImageSnapshot()); |
| 144 | canvas->drawImage(image, 10, 10, NULL); |
| 145 | |
| 146 | SkAutoTUnref<SkSurface> surf2(image->newSurface(info, NULL)); |
| 147 | drawInto(surf2->getCanvas()); |
| 148 | |
| 149 | // Assert that the props were communicated transitively through the first image |
| 150 | SkASSERT(equal(surf->props(), surf2->props())); |
| 151 | |
| 152 | SkAutoTUnref<SkImage> image2(surf2->newImageSnapshot()); |
| 153 | canvas->drawImage(image2, 10 + SkIntToScalar(image->width()) + 10, 10, NULL); |
| 154 | } |
| 155 | |
| 156 | private: |
| 157 | typedef GM INHERITED; |
| 158 | }; |
| 159 | DEF_GM( return new NewSurfaceGM ) |
| 160 | |