gfx/2d/convolver.h

Tue, 06 Jan 2015 21:39:09 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Tue, 06 Jan 2015 21:39:09 +0100
branch
TOR_BUG_9701
changeset 8
97036ab72558
permissions
-rw-r--r--

Conditionally force memory storage according to privacy.thirdparty.isolate;
This solves Tor bug #9701, complying with disk avoidance documented in
https://www.torproject.org/projects/torbrowser/design/#disk-avoidance.

michael@0 1 // Copyright (c) 2006-2012 The Chromium Authors. All rights reserved.
michael@0 2 //
michael@0 3 // Redistribution and use in source and binary forms, with or without
michael@0 4 // modification, are permitted provided that the following conditions
michael@0 5 // are met:
michael@0 6 // * Redistributions of source code must retain the above copyright
michael@0 7 // notice, this list of conditions and the following disclaimer.
michael@0 8 // * Redistributions in binary form must reproduce the above copyright
michael@0 9 // notice, this list of conditions and the following disclaimer in
michael@0 10 // the documentation and/or other materials provided with the
michael@0 11 // distribution.
michael@0 12 // * Neither the name of Google, Inc. nor the names of its contributors
michael@0 13 // may be used to endorse or promote products derived from this
michael@0 14 // software without specific prior written permission.
michael@0 15 //
michael@0 16 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
michael@0 17 // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
michael@0 18 // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
michael@0 19 // FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
michael@0 20 // COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
michael@0 21 // INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
michael@0 22 // BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
michael@0 23 // OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
michael@0 24 // AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
michael@0 25 // OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
michael@0 26 // OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
michael@0 27 // SUCH DAMAGE.
michael@0 28
michael@0 29 #ifndef SKIA_EXT_CONVOLVER_H_
michael@0 30 #define SKIA_EXT_CONVOLVER_H_
michael@0 31
michael@0 32 #include <cmath>
michael@0 33 #include <vector>
michael@0 34
michael@0 35 #include "base/basictypes.h"
michael@0 36 #include "base/cpu.h"
michael@0 37 #include "mozilla/Assertions.h"
michael@0 38 #include "skia/SkTypes.h"
michael@0 39
michael@0 40 // avoid confusion with Mac OS X's math library (Carbon)
michael@0 41 #if defined(__APPLE__)
michael@0 42 #undef FloatToFixed
michael@0 43 #undef FixedToFloat
michael@0 44 #endif
michael@0 45
michael@0 46 namespace skia {
michael@0 47
michael@0 48 // Represents a filter in one dimension. Each output pixel has one entry in this
michael@0 49 // object for the filter values contributing to it. You build up the filter
michael@0 50 // list by calling AddFilter for each output pixel (in order).
michael@0 51 //
michael@0 52 // We do 2-dimensional convolution by first convolving each row by one
michael@0 53 // ConvolutionFilter1D, then convolving each column by another one.
michael@0 54 //
michael@0 55 // Entries are stored in fixed point, shifted left by kShiftBits.
michael@0 56 class ConvolutionFilter1D {
michael@0 57 public:
michael@0 58 typedef short Fixed;
michael@0 59
michael@0 60 // The number of bits that fixed point values are shifted by.
michael@0 61 enum { kShiftBits = 14 };
michael@0 62
michael@0 63 ConvolutionFilter1D();
michael@0 64 ~ConvolutionFilter1D();
michael@0 65
michael@0 66 // Convert between floating point and our fixed point representation.
michael@0 67 static Fixed FloatToFixed(float f) {
michael@0 68 return static_cast<Fixed>(f * (1 << kShiftBits));
michael@0 69 }
michael@0 70 static unsigned char FixedToChar(Fixed x) {
michael@0 71 return static_cast<unsigned char>(x >> kShiftBits);
michael@0 72 }
michael@0 73 static float FixedToFloat(Fixed x) {
michael@0 74 // The cast relies on Fixed being a short, implying that on
michael@0 75 // the platforms we care about all (16) bits will fit into
michael@0 76 // the mantissa of a (32-bit) float.
michael@0 77 static_assert(sizeof(Fixed) == 2,
michael@0 78 "fixed type should fit in float mantissa");
michael@0 79 float raw = static_cast<float>(x);
michael@0 80 return ldexpf(raw, -kShiftBits);
michael@0 81 }
michael@0 82
michael@0 83 // Returns the maximum pixel span of a filter.
michael@0 84 int max_filter() const { return max_filter_; }
michael@0 85
michael@0 86 // Returns the number of filters in this filter. This is the dimension of the
michael@0 87 // output image.
michael@0 88 int num_values() const { return static_cast<int>(filters_.size()); }
michael@0 89
michael@0 90 // Appends the given list of scaling values for generating a given output
michael@0 91 // pixel. |filter_offset| is the distance from the edge of the image to where
michael@0 92 // the scaling factors start. The scaling factors apply to the source pixels
michael@0 93 // starting from this position, and going for the next |filter_length| pixels.
michael@0 94 //
michael@0 95 // You will probably want to make sure your input is normalized (that is,
michael@0 96 // all entries in |filter_values| sub to one) to prevent affecting the overall
michael@0 97 // brighness of the image.
michael@0 98 //
michael@0 99 // The filter_length must be > 0.
michael@0 100 //
michael@0 101 // This version will automatically convert your input to fixed point.
michael@0 102 void AddFilter(int filter_offset,
michael@0 103 const float* filter_values,
michael@0 104 int filter_length);
michael@0 105
michael@0 106 // Same as the above version, but the input is already fixed point.
michael@0 107 void AddFilter(int filter_offset,
michael@0 108 const Fixed* filter_values,
michael@0 109 int filter_length);
michael@0 110
michael@0 111 // Retrieves a filter for the given |value_offset|, a position in the output
michael@0 112 // image in the direction we're convolving. The offset and length of the
michael@0 113 // filter values are put into the corresponding out arguments (see AddFilter
michael@0 114 // above for what these mean), and a pointer to the first scaling factor is
michael@0 115 // returned. There will be |filter_length| values in this array.
michael@0 116 inline const Fixed* FilterForValue(int value_offset,
michael@0 117 int* filter_offset,
michael@0 118 int* filter_length) const {
michael@0 119 const FilterInstance& filter = filters_[value_offset];
michael@0 120 *filter_offset = filter.offset;
michael@0 121 *filter_length = filter.length;
michael@0 122 if (filter.length == 0) {
michael@0 123 return NULL;
michael@0 124 }
michael@0 125 return &filter_values_[filter.data_location];
michael@0 126 }
michael@0 127
michael@0 128
michael@0 129 inline void PaddingForSIMD(int padding_count) {
michael@0 130 // Padding |padding_count| of more dummy coefficients after the coefficients
michael@0 131 // of last filter to prevent SIMD instructions which load 8 or 16 bytes
michael@0 132 // together to access invalid memory areas. We are not trying to align the
michael@0 133 // coefficients right now due to the opaqueness of <vector> implementation.
michael@0 134 // This has to be done after all |AddFilter| calls.
michael@0 135 for (int i = 0; i < padding_count; ++i)
michael@0 136 filter_values_.push_back(static_cast<Fixed>(0));
michael@0 137 }
michael@0 138
michael@0 139 private:
michael@0 140 struct FilterInstance {
michael@0 141 // Offset within filter_values for this instance of the filter.
michael@0 142 int data_location;
michael@0 143
michael@0 144 // Distance from the left of the filter to the center. IN PIXELS
michael@0 145 int offset;
michael@0 146
michael@0 147 // Number of values in this filter instance.
michael@0 148 int length;
michael@0 149 };
michael@0 150
michael@0 151 // Stores the information for each filter added to this class.
michael@0 152 std::vector<FilterInstance> filters_;
michael@0 153
michael@0 154 // We store all the filter values in this flat list, indexed by
michael@0 155 // |FilterInstance.data_location| to avoid the mallocs required for storing
michael@0 156 // each one separately.
michael@0 157 std::vector<Fixed> filter_values_;
michael@0 158
michael@0 159 // The maximum size of any filter we've added.
michael@0 160 int max_filter_;
michael@0 161 };
michael@0 162
michael@0 163 // Does a two-dimensional convolution on the given source image.
michael@0 164 //
michael@0 165 // It is assumed the source pixel offsets referenced in the input filters
michael@0 166 // reference only valid pixels, so the source image size is not required. Each
michael@0 167 // row of the source image starts |source_byte_row_stride| after the previous
michael@0 168 // one (this allows you to have rows with some padding at the end).
michael@0 169 //
michael@0 170 // The result will be put into the given output buffer. The destination image
michael@0 171 // size will be xfilter.num_values() * yfilter.num_values() pixels. It will be
michael@0 172 // in rows of exactly xfilter.num_values() * 4 bytes.
michael@0 173 //
michael@0 174 // |source_has_alpha| is a hint that allows us to avoid doing computations on
michael@0 175 // the alpha channel if the image is opaque. If you don't know, set this to
michael@0 176 // true and it will work properly, but setting this to false will be a few
michael@0 177 // percent faster if you know the image is opaque.
michael@0 178 //
michael@0 179 // The layout in memory is assumed to be 4-bytes per pixel in B-G-R-A order
michael@0 180 // (this is ARGB when loaded into 32-bit words on a little-endian machine).
michael@0 181 void BGRAConvolve2D(const unsigned char* source_data,
michael@0 182 int source_byte_row_stride,
michael@0 183 bool source_has_alpha,
michael@0 184 const ConvolutionFilter1D& xfilter,
michael@0 185 const ConvolutionFilter1D& yfilter,
michael@0 186 int output_byte_row_stride,
michael@0 187 unsigned char* output,
michael@0 188 bool use_sse2);
michael@0 189 } // namespace skia
michael@0 190
michael@0 191 #endif // SKIA_EXT_CONVOLVER_H_

mercurial