OLD | NEW |
1 /* | 1 /* |
2 * Copyright 2011 Google Inc. | 2 * Copyright 2011 Google Inc. |
3 * | 3 * |
4 * Use of this source code is governed by a BSD-style license that can be | 4 * Use of this source code is governed by a BSD-style license that can be |
5 * found in the LICENSE file. | 5 * found in the LICENSE file. |
6 */ | 6 */ |
7 | 7 |
8 #ifndef SkImageFilter_DEFINED | 8 #ifndef SkImageFilter_DEFINED |
9 #define SkImageFilter_DEFINED | 9 #define SkImageFilter_DEFINED |
10 | 10 |
(...skipping 93 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
104 private: | 104 private: |
105 SkRect fRect; | 105 SkRect fRect; |
106 uint32_t fFlags; | 106 uint32_t fFlags; |
107 }; | 107 }; |
108 | 108 |
109 enum TileUsage { | 109 enum TileUsage { |
110 kPossible_TileUsage, //!< the created device may be drawn tiled | 110 kPossible_TileUsage, //!< the created device may be drawn tiled |
111 kNever_TileUsage, //!< the created device will never be drawn tile
d | 111 kNever_TileUsage, //!< the created device will never be drawn tile
d |
112 }; | 112 }; |
113 | 113 |
114 class Proxy { | |
115 public: | |
116 virtual ~Proxy() {} | |
117 | |
118 virtual SkBaseDevice* createDevice(int width, int height, | |
119 TileUsage usage = kNever_TileUsage) =
0; | |
120 }; | |
121 | |
122 class DeviceProxy : public Proxy { | |
123 public: | |
124 DeviceProxy(SkBaseDevice* device) : fDevice(device) {} | |
125 | |
126 SkBaseDevice* createDevice(int width, int height, | |
127 TileUsage usage = kNever_TileUsage) override; | |
128 | |
129 private: | |
130 SkBaseDevice* fDevice; | |
131 }; | |
132 | |
133 /** | 114 /** |
134 * Request a new (result) image to be created from the src image. | 115 * Request a new (result) image to be created from the src image. |
135 * | 116 * |
136 * The context contains the environment in which the filter is occurring. | 117 * The context contains the environment in which the filter is occurring. |
137 * It includes the clip bounds, CTM and cache. | 118 * It includes the clip bounds, CTM and cache. |
138 * | 119 * |
139 * Offset is the amount to translate the resulting image relative to the | 120 * Offset is the amount to translate the resulting image relative to the |
140 * src when it is drawn. This is an out-param. | 121 * src when it is drawn. This is an out-param. |
141 * | 122 * |
142 * If the result image cannot be created, return null, in which case | 123 * If the result image cannot be created, return null, in which case |
(...skipping 18 matching lines...) Expand all Loading... |
161 * be exact, but should never be smaller than the real answer. The default | 142 * be exact, but should never be smaller than the real answer. The default |
162 * implementation recursively unions all input bounds, or returns the | 143 * implementation recursively unions all input bounds, or returns the |
163 * source rect if no inputs. | 144 * source rect if no inputs. |
164 */ | 145 */ |
165 SkIRect filterBounds(const SkIRect& src, const SkMatrix& ctm, | 146 SkIRect filterBounds(const SkIRect& src, const SkMatrix& ctm, |
166 MapDirection = kReverse_MapDirection) const; | 147 MapDirection = kReverse_MapDirection) const; |
167 | 148 |
168 #if SK_SUPPORT_GPU | 149 #if SK_SUPPORT_GPU |
169 static sk_sp<SkSpecialImage> DrawWithFP(GrContext* context, | 150 static sk_sp<SkSpecialImage> DrawWithFP(GrContext* context, |
170 sk_sp<GrFragmentProcessor> fp, | 151 sk_sp<GrFragmentProcessor> fp, |
171 const SkIRect& bounds, | 152 const SkIRect& bounds); |
172 SkImageFilter::Proxy* proxy); | |
173 #endif | 153 #endif |
174 | 154 |
175 /** | 155 /** |
176 * Returns whether this image filter is a color filter and puts the color f
ilter into the | 156 * Returns whether this image filter is a color filter and puts the color f
ilter into the |
177 * "filterPtr" parameter if it can. Does nothing otherwise. | 157 * "filterPtr" parameter if it can. Does nothing otherwise. |
178 * If this returns false, then the filterPtr is unchanged. | 158 * If this returns false, then the filterPtr is unchanged. |
179 * If this returns true, then if filterPtr is not null, it must be set to a
ref'd colorfitler | 159 * If this returns true, then if filterPtr is not null, it must be set to a
ref'd colorfitler |
180 * (i.e. it may not be set to NULL). | 160 * (i.e. it may not be set to NULL). |
181 */ | 161 */ |
182 bool isColorFilterNode(SkColorFilter** filterPtr) const { | 162 bool isColorFilterNode(SkColorFilter** filterPtr) const { |
(...skipping 27 matching lines...) Expand all Loading... |
210 return fInputs[i].get(); | 190 return fInputs[i].get(); |
211 } | 191 } |
212 | 192 |
213 /** | 193 /** |
214 * Returns whether any edges of the crop rect have been set. The crop | 194 * Returns whether any edges of the crop rect have been set. The crop |
215 * rect is set at construction time, and determines which pixels from the | 195 * rect is set at construction time, and determines which pixels from the |
216 * input image will be processed, and which pixels in the output image will
be allowed. | 196 * input image will be processed, and which pixels in the output image will
be allowed. |
217 * The size of the crop rect should be | 197 * The size of the crop rect should be |
218 * used as the size of the destination image. The origin of this rect | 198 * used as the size of the destination image. The origin of this rect |
219 * should be used to offset access to the input images, and should also | 199 * should be used to offset access to the input images, and should also |
220 * be added to the "offset" parameter in onFilterImage and | 200 * be added to the "offset" parameter in onFilterImage. |
221 * filterImageGPU(). (The latter ensures that the resulting buffer is | |
222 * drawn in the correct location.) | |
223 */ | 201 */ |
224 bool cropRectIsSet() const { return fCropRect.flags() != 0x0; } | 202 bool cropRectIsSet() const { return fCropRect.flags() != 0x0; } |
225 | 203 |
226 CropRect getCropRect() const { return fCropRect; } | 204 CropRect getCropRect() const { return fCropRect; } |
227 | 205 |
228 // Default impl returns union of all input bounds. | 206 // Default impl returns union of all input bounds. |
229 virtual SkRect computeFastBounds(const SkRect&) const; | 207 virtual SkRect computeFastBounds(const SkRect&) const; |
230 | 208 |
231 // Can this filter DAG compute the resulting bounds of an object-space recta
ngle? | 209 // Can this filter DAG compute the resulting bounds of an object-space recta
ngle? |
232 bool canComputeFastBounds() const; | 210 bool canComputeFastBounds() const; |
(...skipping 17 matching lines...) Expand all Loading... |
250 SkFilterQuality, | 228 SkFilterQuality, |
251 sk_sp<SkImageFilter> input); | 229 sk_sp<SkImageFilter> input); |
252 #ifdef SK_SUPPORT_LEGACY_IMAGEFILTER_PTR | 230 #ifdef SK_SUPPORT_LEGACY_IMAGEFILTER_PTR |
253 static SkImageFilter* CreateMatrixFilter(const SkMatrix& matrix, | 231 static SkImageFilter* CreateMatrixFilter(const SkMatrix& matrix, |
254 SkFilterQuality filterQuality, | 232 SkFilterQuality filterQuality, |
255 SkImageFilter* input = nullptr) { | 233 SkImageFilter* input = nullptr) { |
256 return MakeMatrixFilter(matrix, filterQuality, sk_ref_sp<SkImageFilter>(
input)).release(); | 234 return MakeMatrixFilter(matrix, filterQuality, sk_ref_sp<SkImageFilter>(
input)).release(); |
257 } | 235 } |
258 #endif | 236 #endif |
259 | 237 |
260 | |
261 sk_sp<SkSpecialImage> filterInput(int index, | |
262 SkSpecialImage* src, | |
263 const Context&, | |
264 SkIPoint* offset) const; | |
265 | |
266 SK_TO_STRING_PUREVIRT() | 238 SK_TO_STRING_PUREVIRT() |
267 SK_DEFINE_FLATTENABLE_TYPE(SkImageFilter) | 239 SK_DEFINE_FLATTENABLE_TYPE(SkImageFilter) |
268 | 240 |
269 protected: | 241 protected: |
270 class Common { | 242 class Common { |
271 public: | 243 public: |
272 /** | 244 /** |
273 * Attempt to unflatten the cropRect and the expected number of input f
ilters. | 245 * Attempt to unflatten the cropRect and the expected number of input f
ilters. |
274 * If any number of input filters is valid, pass -1. | 246 * If any number of input filters is valid, pass -1. |
275 * If this fails (i.e. corrupt buffer or contents) then return false an
d common will | 247 * If this fails (i.e. corrupt buffer or contents) then return false an
d common will |
(...skipping 37 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
313 * to perform image filtering. | 285 * to perform image filtering. |
314 * | 286 * |
315 * src is the original primitive bitmap. If the filter has a connected | 287 * src is the original primitive bitmap. If the filter has a connected |
316 * input, it should recurse on that input and use that in place of src. | 288 * input, it should recurse on that input and use that in place of src. |
317 * | 289 * |
318 * The matrix is the current matrix on the canvas. | 290 * The matrix is the current matrix on the canvas. |
319 * | 291 * |
320 * Offset is the amount to translate the resulting image relative to the | 292 * Offset is the amount to translate the resulting image relative to the |
321 * src when it is drawn. This is an out-param. | 293 * src when it is drawn. This is an out-param. |
322 * | 294 * |
323 * If the result image cannot be created, this should false, in which | 295 * If the result image cannot be created (either because of error or if, sa
y, the result |
324 * case both the result and offset parameters will be ignored by the | 296 * is entirely clipped out), this should return nullptr. |
325 * caller. | 297 * Callers that affect transparent black should explicitly handle nullptr |
| 298 * results and press on. In the error case this behavior will produce a bet
ter result |
| 299 * than nothing and is necessary for the clipped out case. |
| 300 * If the return value is nullptr then offset should be ignored. |
326 */ | 301 */ |
327 virtual bool onFilterImageDeprecated(Proxy*, const SkBitmap& src, const Cont
ext&, | |
328 SkBitmap* result, SkIPoint* offset) con
st; | |
329 | |
330 virtual sk_sp<SkSpecialImage> onFilterImage(SkSpecialImage* src, const Conte
xt&, | 302 virtual sk_sp<SkSpecialImage> onFilterImage(SkSpecialImage* src, const Conte
xt&, |
331 SkIPoint* offset) const; | 303 SkIPoint* offset) const = 0; |
332 | 304 |
333 /** | 305 /** |
334 * This function recurses into its inputs with the given rect (first | 306 * This function recurses into its inputs with the given rect (first |
335 * argument), calls filterBounds() with the given map direction on each, | 307 * argument), calls filterBounds() with the given map direction on each, |
336 * and returns the union of those results. If a derived class has special | 308 * and returns the union of those results. If a derived class has special |
337 * recursion requirements (e.g., it has an input which does not participate | 309 * recursion requirements (e.g., it has an input which does not participate |
338 * in bounds computation), it can be overridden here. | 310 * in bounds computation), it can be overridden here. |
339 * | 311 * |
340 * Note that this function is *not* responsible for mapping the rect for | 312 * Note that this function is *not* responsible for mapping the rect for |
341 * this node's filter bounds requirements (i.e., calling | 313 * this node's filter bounds requirements (i.e., calling |
(...skipping 10 matching lines...) Expand all Loading... |
352 * kReverse_MapDirection is used to determine which pixels of the | 324 * kReverse_MapDirection is used to determine which pixels of the |
353 * input(s) would be required to fill the given destination rect | 325 * input(s) would be required to fill the given destination rect |
354 * (e.g., clip bounds). NOTE: these operations may not be the | 326 * (e.g., clip bounds). NOTE: these operations may not be the |
355 * inverse of the other. For example, blurring expands the given rect | 327 * inverse of the other. For example, blurring expands the given rect |
356 * in both forward and reverse directions. Unlike | 328 * in both forward and reverse directions. Unlike |
357 * onFilterBounds(), this function is non-recursive. | 329 * onFilterBounds(), this function is non-recursive. |
358 */ | 330 */ |
359 virtual SkIRect onFilterNodeBounds(const SkIRect&, const SkMatrix&, MapDirec
tion) const; | 331 virtual SkIRect onFilterNodeBounds(const SkIRect&, const SkMatrix&, MapDirec
tion) const; |
360 | 332 |
361 // Helper function which invokes filter processing on the input at the | 333 // Helper function which invokes filter processing on the input at the |
362 // specified "index". If the input is null, it leaves "result" and | 334 // specified "index". If the input is null, it returns "src" and leaves |
363 // "offset" untouched, and returns true. If the input is non-null, it | 335 // "offset" untouched. If the input is non-null, it |
364 // calls filterImage() on that input, and returns true on success. | 336 // calls filterImage() on that input, and returns the result. |
365 // i.e., return !getInput(index) || getInput(index)->filterImage(...); | 337 sk_sp<SkSpecialImage> filterInput(int index, |
366 bool filterInputDeprecated(int index, Proxy*, const SkBitmap& src, const Con
text&, | 338 SkSpecialImage* src, |
367 SkBitmap* result, SkIPoint* offset) const; | 339 const Context&, |
| 340 SkIPoint* offset) const; |
368 | 341 |
369 /** | 342 /** |
370 * Return true (and return a ref'd colorfilter) if this node in the DAG is
just a | 343 * Return true (and return a ref'd colorfilter) if this node in the DAG is
just a |
371 * colorfilter w/o CropRect constraints. | 344 * colorfilter w/o CropRect constraints. |
372 */ | 345 */ |
373 virtual bool onIsColorFilterNode(SkColorFilter** /*filterPtr*/) const { | 346 virtual bool onIsColorFilterNode(SkColorFilter** /*filterPtr*/) const { |
374 return false; | 347 return false; |
375 } | 348 } |
376 | 349 |
377 /** Given a "srcBounds" rect, computes destination bounds for this filter. | 350 /** Given a "srcBounds" rect, computes destination bounds for this filter. |
(...skipping 24 matching lines...) Expand all Loading... |
402 * filter requires by calling this node's | 375 * filter requires by calling this node's |
403 * onFilterNodeBounds(..., kReverse_MapDirection). | 376 * onFilterNodeBounds(..., kReverse_MapDirection). |
404 */ | 377 */ |
405 Context mapContext(const Context& ctx) const; | 378 Context mapContext(const Context& ctx) const; |
406 | 379 |
407 private: | 380 private: |
408 friend class SkGraphics; | 381 friend class SkGraphics; |
409 static void PurgeCache(); | 382 static void PurgeCache(); |
410 | 383 |
411 void init(sk_sp<SkImageFilter>* inputs, int inputCount, const CropRect* crop
Rect); | 384 void init(sk_sp<SkImageFilter>* inputs, int inputCount, const CropRect* crop
Rect); |
412 bool filterImageDeprecated(Proxy*, const SkBitmap& src, const Context&, | |
413 SkBitmap* result, SkIPoint* offset) const; | |
414 | 385 |
415 bool usesSrcInput() const { return fUsesSrcInput; } | 386 bool usesSrcInput() const { return fUsesSrcInput; } |
416 virtual bool affectsTransparentBlack() const { return false; } | 387 virtual bool affectsTransparentBlack() const { return false; } |
417 | 388 |
418 SkAutoSTArray<2, sk_sp<SkImageFilter>> fInputs; | 389 SkAutoSTArray<2, sk_sp<SkImageFilter>> fInputs; |
419 | 390 |
420 bool fUsesSrcInput; | 391 bool fUsesSrcInput; |
421 CropRect fCropRect; | 392 CropRect fCropRect; |
422 uint32_t fUniqueID; // Globally unique | 393 uint32_t fUniqueID; // Globally unique |
423 mutable SkTArray<Cache::Key> fCacheKeys; | 394 mutable SkTArray<Cache::Key> fCacheKeys; |
424 mutable SkMutex fMutex; | 395 mutable SkMutex fMutex; |
425 typedef SkFlattenable INHERITED; | 396 typedef SkFlattenable INHERITED; |
426 }; | 397 }; |
427 | 398 |
428 /** | 399 /** |
429 * Helper to unflatten the common data, and return NULL if we fail. | 400 * Helper to unflatten the common data, and return NULL if we fail. |
430 */ | 401 */ |
431 #define SK_IMAGEFILTER_UNFLATTEN_COMMON(localVar, expectedCount) \ | 402 #define SK_IMAGEFILTER_UNFLATTEN_COMMON(localVar, expectedCount) \ |
432 Common localVar; \ | 403 Common localVar; \ |
433 do { \ | 404 do { \ |
434 if (!localVar.unflatten(buffer, expectedCount)) { \ | 405 if (!localVar.unflatten(buffer, expectedCount)) { \ |
435 return NULL; \ | 406 return NULL; \ |
436 } \ | 407 } \ |
437 } while (0) | 408 } while (0) |
438 | 409 |
439 #endif | 410 #endif |
OLD | NEW |