Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(242)

Side by Side Diff: src/core/SkImageFilter.cpp

Issue 1218993002: refactor code to apply the croprect (Closed) Base URL: https://skia.googlesource.com/skia.git@master
Patch Set: Created 5 years, 5 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « include/core/SkImageFilter.h ('k') | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 /* 1 /*
2 * Copyright 2012 The Android Open Source Project 2 * Copyright 2012 The Android Open Source Project
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 #include "SkImageFilter.h" 8 #include "SkImageFilter.h"
9 9
10 #include "SkBitmap.h" 10 #include "SkBitmap.h"
(...skipping 15 matching lines...) Expand all
26 #include "SkGrPixelRef.h" 26 #include "SkGrPixelRef.h"
27 #include "SkGr.h" 27 #include "SkGr.h"
28 #endif 28 #endif
29 29
30 #ifdef SK_BUILD_FOR_IOS 30 #ifdef SK_BUILD_FOR_IOS
31 enum { kDefaultCacheSize = 2 * 1024 * 1024 }; 31 enum { kDefaultCacheSize = 2 * 1024 * 1024 };
32 #else 32 #else
33 enum { kDefaultCacheSize = 128 * 1024 * 1024 }; 33 enum { kDefaultCacheSize = 128 * 1024 * 1024 };
34 #endif 34 #endif
35 35
36 #ifndef SK_IGNORE_TO_STRING
37 void SkImageFilter::CropRect::toString(SkString* str) const {
38 if (!fFlags) {
39 return;
40 }
41
42 str->appendf("cropRect (");
43 if (fFlags & CropRect::kHasLeft_CropEdge) {
44 str->appendf("%.2f, ", fRect.fLeft);
45 } else {
46 str->appendf("X, ");
47 }
48 if (fFlags & CropRect::kHasTop_CropEdge) {
49 str->appendf("%.2f, ", fRect.fTop);
50 } else {
51 str->appendf("X, ");
52 }
53 if (fFlags & CropRect::kHasRight_CropEdge) {
54 str->appendf("%.2f, ", fRect.fRight);
55 } else {
56 str->appendf("X, ");
57 }
58 if (fFlags & CropRect::kHasBottom_CropEdge) {
59 str->appendf("%.2f", fRect.fBottom);
60 } else {
61 str->appendf("X");
62 }
63 str->appendf(") ");
64 }
65 #endif
66
67 bool SkImageFilter::CropRect::applyTo(const SkIRect& imageBounds, const Context& ctx,
68 SkIRect* cropped) const {
69 *cropped = imageBounds;
70 if (fFlags) {
71 SkRect devCropR;
72 ctx.ctm().mapRect(&devCropR, fRect);
73 const SkIRect devICropR = devCropR.roundOut();
74
75 // Compute the left/top first, in case we have to read them to compute r ight/bottom
76 if (fFlags & kHasLeft_CropEdge) {
77 cropped->fLeft = devICropR.fLeft;
78 }
79 if (fFlags & kHasTop_CropEdge) {
80 cropped->fTop = devICropR.fTop;
81 }
82 if (fFlags & kHasRight_CropEdge) {
83 cropped->fRight = cropped->fLeft + devICropR.width();
84 }
85 if (fFlags & kHasBottom_CropEdge) {
86 cropped->fBottom = cropped->fTop + devICropR.height();
87 }
88 }
89 return cropped->intersect(ctx.clipBounds());
90 }
91
92 //////////////////////////////////////////////////////////////////////////////// ///////////////////
93
36 static int32_t next_image_filter_unique_id() { 94 static int32_t next_image_filter_unique_id() {
37 static int32_t gImageFilterUniqueID; 95 static int32_t gImageFilterUniqueID;
38 96
39 // Never return 0. 97 // Never return 0.
40 int32_t id; 98 int32_t id;
41 do { 99 do {
42 id = sk_atomic_inc(&gImageFilterUniqueID) + 1; 100 id = sk_atomic_inc(&gImageFilterUniqueID) + 1;
43 } while (0 == id); 101 } while (0 == id);
44 return id; 102 return id;
45 } 103 }
(...skipping 245 matching lines...) Expand 10 before | Expand all | Expand 10 after
291 } 349 }
292 #endif 350 #endif
293 return false; 351 return false;
294 } 352 }
295 353
296 bool SkImageFilter::applyCropRect(const Context& ctx, const SkBitmap& src, 354 bool SkImageFilter::applyCropRect(const Context& ctx, const SkBitmap& src,
297 const SkIPoint& srcOffset, SkIRect* bounds) co nst { 355 const SkIPoint& srcOffset, SkIRect* bounds) co nst {
298 SkIRect srcBounds; 356 SkIRect srcBounds;
299 src.getBounds(&srcBounds); 357 src.getBounds(&srcBounds);
300 srcBounds.offset(srcOffset); 358 srcBounds.offset(srcOffset);
301 SkRect cropRect; 359 return fCropRect.applyTo(srcBounds, ctx, bounds);
302 ctx.ctm().mapRect(&cropRect, fCropRect.rect());
303 const SkIRect cropRectI = cropRect.roundOut();
304 uint32_t flags = fCropRect.flags();
305 if (flags & CropRect::kHasLeft_CropEdge) {
306 srcBounds.fLeft = cropRectI.fLeft;
307 }
308 if (flags & CropRect::kHasTop_CropEdge) {
309 srcBounds.fTop = cropRectI.fTop;
310 }
311 if (flags & CropRect::kHasRight_CropEdge) {
312 srcBounds.fRight = srcBounds.fLeft + cropRectI.width();
313 }
314 if (flags & CropRect::kHasBottom_CropEdge) {
315 srcBounds.fBottom = srcBounds.fTop + cropRectI.height();
316 }
317 if (!srcBounds.intersect(ctx.clipBounds())) {
318 return false;
319 }
320 *bounds = srcBounds;
321 return true;
322 } 360 }
323 361
324 bool SkImageFilter::applyCropRect(const Context& ctx, Proxy* proxy, const SkBitm ap& src, 362 bool SkImageFilter::applyCropRect(const Context& ctx, Proxy* proxy, const SkBitm ap& src,
325 SkIPoint* srcOffset, SkIRect* bounds, SkBitmap * dst) const { 363 SkIPoint* srcOffset, SkIRect* bounds, SkBitmap * dst) const {
326 SkIRect srcBounds; 364 SkIRect srcBounds;
327 src.getBounds(&srcBounds); 365 src.getBounds(&srcBounds);
328 srcBounds.offset(*srcOffset); 366 srcBounds.offset(*srcOffset);
329 SkRect cropRect; 367 if (!fCropRect.applyTo(srcBounds, ctx, bounds)) {
330 ctx.ctm().mapRect(&cropRect, fCropRect.rect());
331 const SkIRect cropRectI = cropRect.roundOut();
332 uint32_t flags = fCropRect.flags();
333 *bounds = srcBounds;
334 if (flags & CropRect::kHasLeft_CropEdge) {
335 bounds->fLeft = cropRectI.fLeft;
336 }
337 if (flags & CropRect::kHasTop_CropEdge) {
338 bounds->fTop = cropRectI.fTop;
339 }
340 if (flags & CropRect::kHasRight_CropEdge) {
341 bounds->fRight = bounds->fLeft + cropRectI.width();
342 }
343 if (flags & CropRect::kHasBottom_CropEdge) {
344 bounds->fBottom = bounds->fTop + cropRectI.height();
345 }
346 if (!bounds->intersect(ctx.clipBounds())) {
347 return false; 368 return false;
348 } 369 }
370
349 if (srcBounds.contains(*bounds)) { 371 if (srcBounds.contains(*bounds)) {
350 *dst = src; 372 *dst = src;
351 return true; 373 return true;
352 } else { 374 } else {
353 SkAutoTUnref<SkBaseDevice> device(proxy->createDevice(bounds->width(), b ounds->height())); 375 SkAutoTUnref<SkBaseDevice> device(proxy->createDevice(bounds->width(), b ounds->height()));
354 if (!device) { 376 if (!device) {
355 return false; 377 return false;
356 } 378 }
357 SkCanvas canvas(device); 379 SkCanvas canvas(device);
358 canvas.clear(0x00000000); 380 canvas.clear(0x00000000);
(...skipping 198 matching lines...) Expand 10 before | Expand all | Expand 10 after
557 } 579 }
558 return dev; 580 return dev;
559 } 581 }
560 582
561 bool SkImageFilter::Proxy::filterImage(const SkImageFilter* filter, const SkBitm ap& src, 583 bool SkImageFilter::Proxy::filterImage(const SkImageFilter* filter, const SkBitm ap& src,
562 const SkImageFilter::Context& ctx, 584 const SkImageFilter::Context& ctx,
563 SkBitmap* result, SkIPoint* offset) { 585 SkBitmap* result, SkIPoint* offset) {
564 return fDevice->filterImage(filter, src, ctx, result, offset); 586 return fDevice->filterImage(filter, src, ctx, result, offset);
565 } 587 }
566 588
OLDNEW
« no previous file with comments | « include/core/SkImageFilter.h ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698