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 "SkGradientShader.h"
10 #include "SkSurface.h"
11 #include "SkSurfaceProps.h"
16 static SkShader* make_shader() {
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);
24 static SkSurface* make_surface(GrContext* ctx, const SkImageInfo& info, SkPixelGeometry geo,
25 int disallowAA, int disallowDither) {
28 flags |= SkSurfaceProps::kDisallowAntiAlias_Flag;
31 flags |= SkSurfaceProps::kDisallowDither_Flag;
34 SkSurfaceProps props(flags, geo);
36 return SkSurface::NewRenderTarget(ctx, SkSurface::kNo_Budgeted, info, 0, &props);
38 return SkSurface::NewRaster(info, &props);
42 static void test_draw(SkCanvas* canvas, const char label[]) {
45 paint.setAntiAlias(true);
46 paint.setLCDRenderText(true);
47 paint.setDither(true);
49 paint.setShader(make_shader())->unref();
50 canvas->drawRect(SkRect::MakeWH(W, H), paint);
51 paint.setShader(NULL);
53 paint.setColor(SK_ColorWHITE);
54 paint.setTextSize(32);
55 paint.setTextAlign(SkPaint::kCenter_Align);
56 canvas->drawText(label, strlen(label), W / 2, H * 3 / 4, paint);
59 class SurfacePropsGM : public skiagm::GM {
64 SkString onShortName() override {
65 return SkString("surfaceprops");
68 SkISize onISize() override {
69 return SkISize::Make(W * 4, H * 5);
72 void onDraw(SkCanvas* canvas) override {
73 GrContext* ctx = canvas->getGrContext();
75 // must be opaque to have a hope of testing LCD text
76 const SkImageInfo info = SkImageInfo::MakeN32(W, H, kOpaque_SkAlphaType);
82 { kUnknown_SkPixelGeometry, "Unknown" },
83 { kRGB_H_SkPixelGeometry, "RGB_H" },
84 { kBGR_H_SkPixelGeometry, "BGR_H" },
85 { kRGB_V_SkPixelGeometry, "RGB_V" },
86 { kBGR_V_SkPixelGeometry, "BGR_V" },
90 for (int disallowAA = 0; disallowAA <= 1; ++disallowAA) {
91 for (int disallowDither = 0; disallowDither <= 1; ++disallowDither) {
93 for (size_t i = 0; i < SK_ARRAY_COUNT(rec); ++i) {
94 SkAutoTUnref<SkSurface> surface(make_surface(ctx, info, rec[i].fGeo,
95 disallowAA, disallowDither));
96 test_draw(surface->getCanvas(), rec[i].fLabel);
97 surface->draw(canvas, x, y, NULL);
106 typedef GM INHERITED;
108 DEF_GM( return new SurfacePropsGM )
111 static bool equal(const SkSurfaceProps& a, const SkSurfaceProps& b) {
112 return a.flags() == b.flags() && a.pixelGeometry() == b.pixelGeometry();
116 class NewSurfaceGM : public skiagm::GM {
121 SkString onShortName() override {
122 return SkString("surfacenew");
125 SkISize onISize() override {
126 return SkISize::Make(300, 140);
129 static void drawInto(SkCanvas* canvas) {
130 canvas->drawColor(SK_ColorRED);
133 void onDraw(SkCanvas* canvas) override {
134 SkImageInfo info = SkImageInfo::MakeN32Premul(100, 100);
136 SkAutoTUnref<SkSurface> surf(canvas->newSurface(info, NULL));
138 surf.reset(SkSurface::NewRaster(info));
140 drawInto(surf->getCanvas());
142 SkAutoTUnref<SkImage> image(surf->newImageSnapshot());
143 canvas->drawImage(image, 10, 10, NULL);
145 SkAutoTUnref<SkSurface> surf2(image->newSurface(info, NULL));
146 drawInto(surf2->getCanvas());
148 // Assert that the props were communicated transitively through the first image
149 SkASSERT(equal(surf->props(), surf2->props()));
151 SkAutoTUnref<SkImage> image2(surf2->newImageSnapshot());
152 canvas->drawImage(image2, 10 + SkIntToScalar(image->width()) + 10, 10, NULL);
156 typedef GM INHERITED;
158 DEF_GM( return new NewSurfaceGM )