Home | History | Annotate | Download | only in output
      1 // Copyright 2013 The Chromium Authors. All rights reserved.
      2 // Use of this source code is governed by a BSD-style license that can be
      3 // found in the LICENSE file.
      4 
      5 #ifndef CC_OUTPUT_FILTER_OPERATION_H_
      6 #define CC_OUTPUT_FILTER_OPERATION_H_
      7 
      8 #include "base/logging.h"
      9 #include "base/memory/scoped_ptr.h"
     10 #include "cc/base/cc_export.h"
     11 #include "third_party/skia/include/core/SkColor.h"
     12 #include "third_party/skia/include/core/SkScalar.h"
     13 #include "ui/gfx/point.h"
     14 
     15 namespace base {
     16 class Value;
     17 }
     18 
     19 namespace cc {
     20 
     21 class CC_EXPORT FilterOperation {
     22  public:
     23   enum FilterType {
     24     GRAYSCALE,
     25     SEPIA,
     26     SATURATE,
     27     HUE_ROTATE,
     28     INVERT,
     29     BRIGHTNESS,
     30     CONTRAST,
     31     OPACITY,
     32     BLUR,
     33     DROP_SHADOW,
     34     COLOR_MATRIX,
     35     ZOOM,
     36     SATURATING_BRIGHTNESS,  // Not used in CSS/SVG.
     37   };
     38 
     39   FilterType type() const { return type_; }
     40 
     41   float amount() const {
     42     DCHECK_NE(type_, COLOR_MATRIX);
     43     return amount_;
     44   }
     45 
     46   gfx::Point drop_shadow_offset() const {
     47     DCHECK_EQ(type_, DROP_SHADOW);
     48     return drop_shadow_offset_;
     49   }
     50 
     51   SkColor drop_shadow_color() const {
     52     DCHECK_EQ(type_, DROP_SHADOW);
     53     return drop_shadow_color_;
     54   }
     55 
     56   const SkScalar* matrix() const {
     57     DCHECK_EQ(type_, COLOR_MATRIX);
     58     return matrix_;
     59   }
     60 
     61   int zoom_inset() const {
     62     DCHECK_EQ(type_, ZOOM);
     63     return zoom_inset_;
     64   }
     65 
     66   static FilterOperation CreateGrayscaleFilter(float amount) {
     67     return FilterOperation(GRAYSCALE, amount);
     68   }
     69 
     70   static FilterOperation CreateSepiaFilter(float amount) {
     71     return FilterOperation(SEPIA, amount);
     72   }
     73 
     74   static FilterOperation CreateSaturateFilter(float amount) {
     75     return FilterOperation(SATURATE, amount);
     76   }
     77 
     78   static FilterOperation CreateHueRotateFilter(float amount) {
     79     return FilterOperation(HUE_ROTATE, amount);
     80   }
     81 
     82   static FilterOperation CreateInvertFilter(float amount) {
     83     return FilterOperation(INVERT, amount);
     84   }
     85 
     86   static FilterOperation CreateBrightnessFilter(float amount) {
     87     return FilterOperation(BRIGHTNESS, amount);
     88   }
     89 
     90   static FilterOperation CreateContrastFilter(float amount) {
     91     return FilterOperation(CONTRAST, amount);
     92   }
     93 
     94   static FilterOperation CreateOpacityFilter(float amount) {
     95     return FilterOperation(OPACITY, amount);
     96   }
     97 
     98   static FilterOperation CreateBlurFilter(float amount) {
     99     return FilterOperation(BLUR, amount);
    100   }
    101 
    102   static FilterOperation CreateDropShadowFilter(gfx::Point offset,
    103                                                 float std_deviation,
    104                                                 SkColor color) {
    105     return FilterOperation(DROP_SHADOW, offset, std_deviation, color);
    106   }
    107 
    108   static FilterOperation CreateColorMatrixFilter(SkScalar matrix[20]) {
    109     return FilterOperation(COLOR_MATRIX, matrix);
    110   }
    111 
    112   static FilterOperation CreateZoomFilter(float amount, int inset) {
    113     return FilterOperation(ZOOM, amount, inset);
    114   }
    115 
    116   static FilterOperation CreateSaturatingBrightnessFilter(float amount) {
    117     return FilterOperation(SATURATING_BRIGHTNESS, amount);
    118   }
    119 
    120   bool operator==(const FilterOperation& other) const;
    121 
    122   bool operator!=(const FilterOperation& other) const {
    123     return !(*this == other);
    124   }
    125 
    126   // Methods for restoring a FilterOperation.
    127   static FilterOperation CreateEmptyFilter() {
    128     return FilterOperation(GRAYSCALE, 0.f);
    129   }
    130 
    131   void set_type(FilterType type) { type_ = type; }
    132 
    133   void set_amount(float amount) {
    134     DCHECK_NE(type_, COLOR_MATRIX);
    135     amount_ = amount;
    136   }
    137 
    138   void set_drop_shadow_offset(gfx::Point offset) {
    139     DCHECK_EQ(type_, DROP_SHADOW);
    140     drop_shadow_offset_ = offset;
    141   }
    142 
    143   void set_drop_shadow_color(SkColor color) {
    144     DCHECK_EQ(type_, DROP_SHADOW);
    145     drop_shadow_color_ = color;
    146   }
    147 
    148   void set_matrix(const SkScalar matrix[20]) {
    149     DCHECK_EQ(type_, COLOR_MATRIX);
    150     for (unsigned i = 0; i < 20; ++i)
    151       matrix_[i] = matrix[i];
    152   }
    153 
    154   void set_zoom_inset(int inset) {
    155     DCHECK_EQ(type_, ZOOM);
    156     zoom_inset_ = inset;
    157   }
    158 
    159   // Given two filters of the same type, returns a filter operation created by
    160   // linearly interpolating a |progress| fraction from |from| to |to|. If either
    161   // |from| or |to| (but not both) is null, it is treated as a no-op filter of
    162   // the same type as the other given filter. If both |from| and |to| are null,
    163   // or if |from| and |to| are non-null but of different types, returns a
    164   // no-op filter.
    165   static FilterOperation Blend(const FilterOperation* from,
    166                                const FilterOperation* to,
    167                                double progress);
    168 
    169   scoped_ptr<base::Value> AsValue() const;
    170 
    171  private:
    172   FilterOperation(FilterType type, float amount);
    173 
    174   FilterOperation(FilterType type,
    175                   gfx::Point offset,
    176                   float stdDeviation,
    177                   SkColor color);
    178 
    179   FilterOperation(FilterType, SkScalar matrix[20]);
    180 
    181   FilterOperation(FilterType type, float amount, int inset);
    182 
    183   FilterType type_;
    184   float amount_;
    185   gfx::Point drop_shadow_offset_;
    186   SkColor drop_shadow_color_;
    187   SkScalar matrix_[20];
    188   int zoom_inset_;
    189 };
    190 
    191 }  // namespace cc
    192 
    193 #endif  // CC_OUTPUT_FILTER_OPERATION_H_
    194