2 * Copyright 2014 Google Inc.
4 * Use of this source code is governed by a BSD-style license that can be
5 * found in the LICENSE file.
9 #include "sk_tool_utils.h"
10 #include "SkGradientShader.h"
11 #include "SkSurface.h"
12 #include "SkSurfaceProps.h"
17 static sk_sp<SkShader> make_shader() {
20 SkPoint pts[] = { { 0, 0 }, { W, H } };
21 SkColor colors[] = { SkColorSetRGB(a, a, a), SkColorSetRGB(b, b, b) };
22 return SkGradientShader::MakeLinear(pts, colors, nullptr, 2, SkShader::kClamp_TileMode);
25 static sk_sp<SkSurface> make_surface(GrContext* ctx, const SkImageInfo& info, SkPixelGeometry geo) {
26 SkSurfaceProps props(0, geo);
28 return SkSurface::MakeRenderTarget(ctx, SkBudgeted::kNo, info, 0, &props);
30 return SkSurface::MakeRaster(info, &props);
34 static void test_draw(SkCanvas* canvas, const char label[]) {
37 paint.setAntiAlias(true);
38 paint.setLCDRenderText(true);
39 paint.setDither(true);
41 paint.setShader(make_shader());
42 canvas->drawRect(SkRect::MakeWH(W, H), paint);
43 paint.setShader(nullptr);
45 paint.setColor(SK_ColorWHITE);
46 paint.setTextSize(32);
47 paint.setTextAlign(SkPaint::kCenter_Align);
48 sk_tool_utils::set_portable_typeface(&paint);
49 canvas->drawString(label, W / 2, H * 3 / 4, paint);
52 class SurfacePropsGM : public skiagm::GM {
57 SkString onShortName() override {
58 return SkString("surfaceprops");
61 SkISize onISize() override {
62 return SkISize::Make(W, H * 5);
65 void onDraw(SkCanvas* canvas) override {
66 GrContext* ctx = canvas->getGrContext();
68 // must be opaque to have a hope of testing LCD text
69 const SkImageInfo info = SkImageInfo::MakeN32(W, H, kOpaque_SkAlphaType);
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" },
84 for (const auto& rec : recs) {
85 auto surface(make_surface(ctx, info, rec.fGeo));
87 SkDebugf("failed to create surface! label: %s", rec.fLabel);
90 test_draw(surface->getCanvas(), rec.fLabel);
91 surface->draw(canvas, x, y, nullptr);
99 DEF_GM( return new SurfacePropsGM )
102 static bool equal(const SkSurfaceProps& a, const SkSurfaceProps& b) {
103 return a.flags() == b.flags() && a.pixelGeometry() == b.pixelGeometry();
107 class NewSurfaceGM : public skiagm::GM {
112 SkString onShortName() override {
113 return SkString("surfacenew");
116 SkISize onISize() override {
117 return SkISize::Make(300, 140);
120 static void drawInto(SkCanvas* canvas) {
121 canvas->drawColor(SK_ColorRED);
124 void onDraw(SkCanvas* canvas) override {
125 SkImageInfo info = SkImageInfo::MakeN32Premul(100, 100);
127 auto surf(canvas->makeSurface(info, nullptr));
129 surf = SkSurface::MakeRaster(info);
131 drawInto(surf->getCanvas());
133 sk_sp<SkImage> image(surf->makeImageSnapshot());
134 canvas->drawImage(image, 10, 10, nullptr);
136 auto surf2(surf->makeSurface(info));
137 drawInto(surf2->getCanvas());
139 // Assert that the props were communicated transitively through the first image
140 SkASSERT(equal(surf->props(), surf2->props()));
142 sk_sp<SkImage> image2(surf2->makeImageSnapshot());
143 canvas->drawImage(image2.get(), 10 + SkIntToScalar(image->width()) + 10, 10, nullptr);
147 typedef GM INHERITED;
149 DEF_GM( return new NewSurfaceGM )
151 ///////////////////////////////////////////////////////////////////////////////////////////////////
153 DEF_SIMPLE_GM(copy_on_write_retain, canvas, 256, 256) {
154 const SkImageInfo info = SkImageInfo::MakeN32Premul(256, 256);
155 sk_sp<SkSurface> surf = canvas->makeSurface(info, nullptr);
157 surf = SkSurface::MakeRaster(info, nullptr);
160 surf->getCanvas()->clear(SK_ColorRED);
161 // its important that image survives longer than the next draw, so the surface will see
162 // an outstanding image, and have to decide if it should retain or discard those pixels
163 sk_sp<SkImage> image = surf->makeImageSnapshot();
165 // normally a clear+opaque should trigger the discard optimization, but since we have a clip
166 // it should not (we need the previous red pixels).
167 surf->getCanvas()->clipRect(SkRect::MakeWH(128, 256));
168 surf->getCanvas()->clear(SK_ColorBLUE);
170 // expect to see two rects: blue | red
171 canvas->drawImage(surf->makeImageSnapshot(), 0, 0, nullptr);
174 DEF_SIMPLE_GM(copy_on_write_savelayer, canvas, 256, 256) {
175 const SkImageInfo info = SkImageInfo::MakeN32Premul(256, 256);
176 sk_sp<SkSurface> surf = canvas->makeSurface(info, nullptr);
178 surf = SkSurface::MakeRaster(info, nullptr);
181 surf->getCanvas()->clear(SK_ColorRED);
182 // its important that image survives longer than the next draw, so the surface will see
183 // an outstanding image, and have to decide if it should retain or discard those pixels
184 sk_sp<SkImage> image = surf->makeImageSnapshot();
186 // now draw into a full-screen layer. This should (a) trigger a copy-on-write, but it should
187 // not trigger discard, even tho its alpha (SK_ColorBLUE) is opaque, since it is in a layer
188 // with a non-opaque paint.
190 paint.setAlpha(0x40);
191 surf->getCanvas()->saveLayer({0, 0, 256, 256}, &paint);
192 surf->getCanvas()->clear(SK_ColorBLUE);
193 surf->getCanvas()->restore();
195 // expect to see two rects: blue blended on red
196 canvas->drawImage(surf->makeImageSnapshot(), 0, 0, nullptr);