blob: 9e39025b0ac304136ee900c0817a7777be7a076c [file] [log] [blame]
robertphillipsa408c8f2016-07-28 09:20:33 -07001/*
2 * Copyright 2016 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 "SkGammaColorFilter.h"
10#include "SkImage.h"
11
12// Fill a width x height block with a horizontal ramp from 0 to 255
13static void draw_grey_ramp(SkCanvas* canvas, int width, int height, int numSteps) {
14 int greyPerStep = SkScalarRoundToInt(255.0f / numSteps);
15 int widthPerStep = SkScalarRoundToInt(width / SkIntToScalar(numSteps));
16
17 SkIRect rect = SkIRect::MakeWH(widthPerStep, height);
18
19 SkPaint paint;
20 int grey = 0;
21 int x = 0;
22 for (int i = 0; i < numSteps-1; ++i) {
23 paint.setColor(SkColorSetARGB(255, grey, grey, grey));
24
25 rect.offsetTo(x, 0);
26 canvas->drawRect(SkRect::Make(rect), paint);
27
28 x += widthPerStep;
29 grey += greyPerStep;
30 }
31
32 paint.setColor(SK_ColorWHITE);
33 rect.setLTRB(x, 0, width, height);
34 canvas->drawRect(SkRect::Make(rect), paint);
35}
36
37static sk_sp<SkImage> create_grey_ramp(int width, int height, int numSteps) {
38 SkBitmap bm;
39 bm.allocN32Pixels(width, height);
40 SkCanvas canvas(bm);
41 canvas.clear(0x0);
42
43 draw_grey_ramp(&canvas, width, height, numSteps);
44
45 return SkImage::MakeFromBitmap(bm);
46}
47
48namespace skiagm {
49
50class GammaColorFilterGM : public GM {
51public:
52 GammaColorFilterGM() {
53 this->setBGColor(SK_ColorBLACK);
54 }
55
56protected:
57
58 SkString onShortName() override {
59 return SkString("gammacolorfilter");
60 }
61
62 SkISize onISize() override {
63 return SkISize::Make(4 * kCellWidth, kCellHeight);
64 }
65
66 void onDraw(SkCanvas* canvas) override {
Brian Osman11052242016-10-27 14:47:55 -040067 GrRenderTargetContext* renderTargetContext =
68 canvas->internal_private_accessTopLayerRenderTargetContext();
69 if (!renderTargetContext) {
robertphillipsa408c8f2016-07-28 09:20:33 -070070 skiagm::GM::DrawGpuOnlyMessage(canvas);
71 return;
72 }
73
74 sk_sp<SkImage> image(create_grey_ramp(kCellWidth, kCellHeight/2, kNumGreySteps));
75
76 // Leftmost is a non-gamma pair
77 draw_grey_ramp(canvas, kCellWidth, kCellHeight/2, kNumGreySteps);
78 canvas->drawImage(image, 0, kCellHeight/2);
79 canvas->translate(SkIntToScalar(image->width()), 0);
80
81 for (auto gamma : { 1.0f, 1.0f / 1.8f, 1.0f / 2.2f }) {
82 SkPaint paint;
83 paint.setColorFilter(SkGammaColorFilter::Make(gamma));
84
85 draw_grey_ramp(canvas, kCellWidth, kCellHeight/2, kNumGreySteps);
86 canvas->drawImage(image, 0, kCellHeight/2, &paint);
87 canvas->translate(SkIntToScalar(image->width()), 0);
88 }
89 }
90
91private:
mtkleindbfd7ab2016-09-01 11:24:54 -070092 static constexpr int kCellWidth = 64;
93 static constexpr int kCellHeight = 64;
94 static constexpr int kNumGreySteps = 16;
robertphillipsa408c8f2016-07-28 09:20:33 -070095
96 typedef GM INHERITED;
97};
98
99//////////////////////////////////////////////////////////////////////////////
100
101DEF_GM(return new GammaColorFilterGM;)
102}