blob: 6323c7047b81e19b0af5f07c2d7b34040da55aee [file] [log] [blame]
reed4a8126e2014-09-22 07:29:03 -07001/*
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
reed1a9b9642016-03-13 14:13:58 -070016static sk_sp<SkShader> make_shader() {
reed4a8126e2014-09-22 07:29:03 -070017 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) };
reed1a9b9642016-03-13 14:13:58 -070021 return SkGradientShader::MakeLinear(pts, colors, nullptr, 2, SkShader::kClamp_TileMode);
reed4a8126e2014-09-22 07:29:03 -070022}
23
reed7c123542016-08-18 09:30:44 -070024static sk_sp<SkSurface> make_surface(GrContext* ctx, const SkImageInfo& info, SkPixelGeometry geo) {
25 SkSurfaceProps props(0, geo);
reed4a8126e2014-09-22 07:29:03 -070026 if (ctx) {
reede8f30622016-03-23 18:59:25 -070027 return SkSurface::MakeRenderTarget(ctx, SkBudgeted::kNo, info, 0, &props);
reed4a8126e2014-09-22 07:29:03 -070028 } else {
reede8f30622016-03-23 18:59:25 -070029 return SkSurface::MakeRaster(info, &props);
reed4a8126e2014-09-22 07:29:03 -070030 }
31}
32
33static 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
reed1a9b9642016-03-13 14:13:58 -070040 paint.setShader(make_shader());
reed4a8126e2014-09-22 07:29:03 -070041 canvas->drawRect(SkRect::MakeWH(W, H), paint);
halcanary96fcdcc2015-08-27 07:41:13 -070042 paint.setShader(nullptr);
reed4a8126e2014-09-22 07:29:03 -070043
44 paint.setColor(SK_ColorWHITE);
45 paint.setTextSize(32);
46 paint.setTextAlign(SkPaint::kCenter_Align);
caryclarkf597c422015-07-28 10:37:53 -070047 sk_tool_utils::set_portable_typeface(&paint);
reed4a8126e2014-09-22 07:29:03 -070048 canvas->drawText(label, strlen(label), W / 2, H * 3 / 4, paint);
49}
50
51class SurfacePropsGM : public skiagm::GM {
52public:
53 SurfacePropsGM() {}
54
55protected:
mtklein36352bf2015-03-25 18:17:31 -070056 SkString onShortName() override {
reed4a8126e2014-09-22 07:29:03 -070057 return SkString("surfaceprops");
58 }
59
mtklein36352bf2015-03-25 18:17:31 -070060 SkISize onISize() override {
reed7c123542016-08-18 09:30:44 -070061 return SkISize::Make(W, H * 5);
reed4a8126e2014-09-22 07:29:03 -070062 }
63
mtklein36352bf2015-03-25 18:17:31 -070064 void onDraw(SkCanvas* canvas) override {
reed4a8126e2014-09-22 07:29:03 -070065 GrContext* ctx = canvas->getGrContext();
66
67 // must be opaque to have a hope of testing LCD text
brianosman0e22eb82016-08-30 07:07:59 -070068 const SkImageInfo info = SkImageInfo::MakeN32(W, H, kOpaque_SkAlphaType);
reed4a8126e2014-09-22 07:29:03 -070069
70 const struct {
71 SkPixelGeometry fGeo;
72 const char* fLabel;
scroggoe6cad6b2016-05-09 13:20:58 -070073 } recs[] = {
reed4a8126e2014-09-22 07:29:03 -070074 { 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 };
halcanary9d524f22016-03-29 09:03:52 -070080
reed4a8126e2014-09-22 07:29:03 -070081 SkScalar x = 0;
reed7c123542016-08-18 09:30:44 -070082 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;
reed4a8126e2014-09-22 07:29:03 -070088 }
reed7c123542016-08-18 09:30:44 -070089 test_draw(surface->getCanvas(), rec.fLabel);
90 surface->draw(canvas, x, y, nullptr);
91 y += H;
reed4a8126e2014-09-22 07:29:03 -070092 }
93 }
94
95private:
96 typedef GM INHERITED;
97};
reed4a8126e2014-09-22 07:29:03 -070098DEF_GM( return new SurfacePropsGM )
reed4af267b2014-11-21 08:46:37 -080099
100#ifdef SK_DEBUG
101static bool equal(const SkSurfaceProps& a, const SkSurfaceProps& b) {
102 return a.flags() == b.flags() && a.pixelGeometry() == b.pixelGeometry();
103}
104#endif
105
106class NewSurfaceGM : public skiagm::GM {
107public:
108 NewSurfaceGM() {}
109
110protected:
mtklein36352bf2015-03-25 18:17:31 -0700111 SkString onShortName() override {
reed4af267b2014-11-21 08:46:37 -0800112 return SkString("surfacenew");
113 }
114
mtklein36352bf2015-03-25 18:17:31 -0700115 SkISize onISize() override {
reed4af267b2014-11-21 08:46:37 -0800116 return SkISize::Make(300, 140);
117 }
118
119 static void drawInto(SkCanvas* canvas) {
120 canvas->drawColor(SK_ColorRED);
121 }
122
mtklein36352bf2015-03-25 18:17:31 -0700123 void onDraw(SkCanvas* canvas) override {
reed4af267b2014-11-21 08:46:37 -0800124 SkImageInfo info = SkImageInfo::MakeN32Premul(100, 100);
125
reede8f30622016-03-23 18:59:25 -0700126 auto surf(canvas->makeSurface(info, nullptr));
127 if (!surf) {
128 surf = SkSurface::MakeRaster(info);
reed4af267b2014-11-21 08:46:37 -0800129 }
130 drawInto(surf->getCanvas());
131
reed9ce9d672016-03-17 10:51:11 -0700132 sk_sp<SkImage> image(surf->makeImageSnapshot());
halcanary96fcdcc2015-08-27 07:41:13 -0700133 canvas->drawImage(image, 10, 10, nullptr);
reed4af267b2014-11-21 08:46:37 -0800134
reede8f30622016-03-23 18:59:25 -0700135 auto surf2(surf->makeSurface(info));
reed4af267b2014-11-21 08:46:37 -0800136 drawInto(surf2->getCanvas());
137
138 // Assert that the props were communicated transitively through the first image
139 SkASSERT(equal(surf->props(), surf2->props()));
140
reed9ce9d672016-03-17 10:51:11 -0700141 sk_sp<SkImage> image2(surf2->makeImageSnapshot());
142 canvas->drawImage(image2.get(), 10 + SkIntToScalar(image->width()) + 10, 10, nullptr);
reed4af267b2014-11-21 08:46:37 -0800143 }
144
145private:
146 typedef GM INHERITED;
147};
148DEF_GM( return new NewSurfaceGM )