blob: f176cddaee26dd43f3514398f99d576e1f097de3 [file] [log] [blame]
bsalomon@google.comb505a122012-05-31 18:40:36 +00001/*
2 * Copyright 2012 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#ifndef Gr1DKernelEffect_DEFINED
9#define Gr1DKernelEffect_DEFINED
10
tomhudson@google.comd0c1a062012-07-12 17:23:52 +000011#include "GrSingleTextureEffect.h"
bsalomon@google.comb4a55b72012-11-02 20:45:37 +000012#include "GrTexture.h"
13#include "SkMatrix.h"
bsalomon@google.comb505a122012-05-31 18:40:36 +000014
15/**
16 * Base class for 1D kernel effects. The kernel operates either in X or Y and
17 * has a pixel radius. The kernel is specified in the src texture's space
18 * and the kernel center is pinned to a texel's center. The radius specifies
19 * the number of texels on either side of the center texel in X or Y that are
20 * read. Since the center pixel is also read, the total width is one larger than
21 * two times the radius.
22 */
bsalomon@google.comb4a55b72012-11-02 20:45:37 +000023
24namespace {
25inline SkMatrix make_texture_matrix(GrTexture* tex) {
26 GrAssert(NULL != tex);
27 SkMatrix mat;
28 mat.setIDiv(tex->width(), tex->height());
29 return mat;
30}
31}
32
tomhudson@google.comd0c1a062012-07-12 17:23:52 +000033class Gr1DKernelEffect : public GrSingleTextureEffect {
bsalomon@google.comb505a122012-05-31 18:40:36 +000034
35public:
36 enum Direction {
37 kX_Direction,
38 kY_Direction,
39 };
40
tomhudson@google.comd0c1a062012-07-12 17:23:52 +000041 Gr1DKernelEffect(GrTexture* texture,
42 Direction direction,
bsalomon@google.comb505a122012-05-31 18:40:36 +000043 int radius)
bsalomon@google.comb4a55b72012-11-02 20:45:37 +000044 : GrSingleTextureEffect(texture, make_texture_matrix(texture))
tomhudson@google.comd0c1a062012-07-12 17:23:52 +000045 , fDirection(direction)
bsalomon@google.comb505a122012-05-31 18:40:36 +000046 , fRadius(radius) {}
47
48 virtual ~Gr1DKernelEffect() {};
49
50 static int WidthFromRadius(int radius) { return 2 * radius + 1; }
51
52 int radius() const { return fRadius; }
53 int width() const { return WidthFromRadius(fRadius); }
54 Direction direction() const { return fDirection; }
55
56private:
57
58 Direction fDirection;
59 int fRadius;
60
tomhudson@google.comd0c1a062012-07-12 17:23:52 +000061 typedef GrSingleTextureEffect INHERITED;
bsalomon@google.comb505a122012-05-31 18:40:36 +000062};
63
64#endif