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

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

Issue 1211583003: add drawImageNine (Closed) Base URL: https://skia.googlesource.com/skia.git@master
Patch Set: address comments from #9 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/utils/SkDeferredCanvas.h ('k') | src/core/SkDevice.cpp » ('j') | 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 2008 The Android Open Source Project 2 * Copyright 2008 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 "SkCanvas.h" 8 #include "SkCanvas.h"
9 #include "SkCanvasPriv.h" 9 #include "SkCanvasPriv.h"
10 #include "SkBitmapDevice.h" 10 #include "SkBitmapDevice.h"
11 #include "SkColorFilter.h" 11 #include "SkColorFilter.h"
12 #include "SkDraw.h" 12 #include "SkDraw.h"
13 #include "SkDrawable.h" 13 #include "SkDrawable.h"
14 #include "SkDrawFilter.h" 14 #include "SkDrawFilter.h"
15 #include "SkDrawLooper.h" 15 #include "SkDrawLooper.h"
16 #include "SkErrorInternals.h" 16 #include "SkErrorInternals.h"
17 #include "SkImage.h" 17 #include "SkImage.h"
18 #include "SkMetaData.h" 18 #include "SkMetaData.h"
19 #include "SkNinePatchIter.h"
19 #include "SkPathOps.h" 20 #include "SkPathOps.h"
20 #include "SkPatchUtils.h" 21 #include "SkPatchUtils.h"
21 #include "SkPicture.h" 22 #include "SkPicture.h"
22 #include "SkRasterClip.h" 23 #include "SkRasterClip.h"
23 #include "SkReadPixelsRec.h" 24 #include "SkReadPixelsRec.h"
24 #include "SkRRect.h" 25 #include "SkRRect.h"
25 #include "SkSmallAllocator.h" 26 #include "SkSmallAllocator.h"
26 #include "SkSurface_Base.h" 27 #include "SkSurface_Base.h"
27 #include "SkTemplates.h" 28 #include "SkTemplates.h"
28 #include "SkTextBlob.h" 29 #include "SkTextBlob.h"
(...skipping 1735 matching lines...) Expand 10 before | Expand all | Expand 10 after
1764 } 1765 }
1765 1766
1766 void SkCanvas::drawImageRect(const SkImage* image, const SkRect* src, const SkRe ct& dst, 1767 void SkCanvas::drawImageRect(const SkImage* image, const SkRect* src, const SkRe ct& dst,
1767 const SkPaint* paint) { 1768 const SkPaint* paint) {
1768 if (dst.isEmpty()) { 1769 if (dst.isEmpty()) {
1769 return; 1770 return;
1770 } 1771 }
1771 this->onDrawImageRect(image, src, dst, paint); 1772 this->onDrawImageRect(image, src, dst, paint);
1772 } 1773 }
1773 1774
1775 void SkCanvas::drawImageNine(const SkImage* image, const SkIRect& center, const SkRect& dst,
1776 const SkPaint* paint) {
1777 if (dst.isEmpty()) {
1778 return;
1779 }
1780 if (!SkNinePatchIter::Valid(image->width(), image->height(), center)) {
1781 this->drawImageRect(image, NULL, dst, paint);
1782 }
1783 this->onDrawImageNine(image, center, dst, paint);
1784 }
1785
1774 void SkCanvas::drawBitmap(const SkBitmap& bitmap, SkScalar dx, SkScalar dy, cons t SkPaint* paint) { 1786 void SkCanvas::drawBitmap(const SkBitmap& bitmap, SkScalar dx, SkScalar dy, cons t SkPaint* paint) {
1775 if (bitmap.empty()) { 1787 if (bitmap.drawsNothing()) {
1776 return; 1788 return;
1777 } 1789 }
1778 this->onDrawBitmap(bitmap, dx, dy, paint); 1790 this->onDrawBitmap(bitmap, dx, dy, paint);
1779 } 1791 }
1780 1792
1781 void SkCanvas::drawBitmapRectToRect(const SkBitmap& bitmap, const SkRect* src, c onst SkRect& dst, 1793 void SkCanvas::drawBitmapRectToRect(const SkBitmap& bitmap, const SkRect* src, c onst SkRect& dst,
1782 const SkPaint* paint, DrawBitmapRectFlags fl ags) { 1794 const SkPaint* paint, DrawBitmapRectFlags fl ags) {
1783 if (bitmap.empty()) { 1795 if (bitmap.drawsNothing() || dst.isEmpty()) {
1784 return; 1796 return;
1785 } 1797 }
1786 this->onDrawBitmapRect(bitmap, src, dst, paint, flags); 1798 this->onDrawBitmapRect(bitmap, src, dst, paint, flags);
1787 } 1799 }
1788 1800
1789 void SkCanvas::drawBitmapNine(const SkBitmap& bitmap, const SkIRect& center, con st SkRect& dst, 1801 void SkCanvas::drawBitmapNine(const SkBitmap& bitmap, const SkIRect& center, con st SkRect& dst,
1790 const SkPaint* paint) { 1802 const SkPaint* paint) {
1791 if (bitmap.empty()) { 1803 if (bitmap.drawsNothing() || dst.isEmpty()) {
1792 return; 1804 return;
1793 } 1805 }
1806 if (!SkNinePatchIter::Valid(bitmap.width(), bitmap.height(), center)) {
1807 this->drawBitmapRectToRect(bitmap, NULL, dst, paint);
1808 }
1794 this->onDrawBitmapNine(bitmap, center, dst, paint); 1809 this->onDrawBitmapNine(bitmap, center, dst, paint);
1795 } 1810 }
1796 1811
1797 void SkCanvas::drawSprite(const SkBitmap& bitmap, int left, int top, const SkPai nt* paint) { 1812 void SkCanvas::drawSprite(const SkBitmap& bitmap, int left, int top, const SkPai nt* paint) {
1798 if (bitmap.empty()) { 1813 if (bitmap.drawsNothing()) {
1799 return; 1814 return;
1800 } 1815 }
1801 this->onDrawSprite(bitmap, left, top, paint); 1816 this->onDrawSprite(bitmap, left, top, paint);
1802 } 1817 }
1803 1818
1804 void SkCanvas::drawAtlas(const SkImage* atlas, const SkRSXform xform[], const Sk Rect tex[], 1819 void SkCanvas::drawAtlas(const SkImage* atlas, const SkRSXform xform[], const Sk Rect tex[],
1805 const SkColor colors[], int count, SkXfermode::Mode mod e, 1820 const SkColor colors[], int count, SkXfermode::Mode mod e,
1806 const SkRect* cull, const SkPaint* paint) { 1821 const SkRect* cull, const SkPaint* paint) {
1807 if (count <= 0) { 1822 if (count <= 0) {
1808 return; 1823 return;
(...skipping 300 matching lines...) Expand 10 before | Expand all | Expand 10 after
2109 LOOPER_END 2124 LOOPER_END
2110 } 2125 }
2111 2126
2112 void SkCanvas::onDrawBitmapRect(const SkBitmap& bitmap, const SkRect* src, const SkRect& dst, 2127 void SkCanvas::onDrawBitmapRect(const SkBitmap& bitmap, const SkRect* src, const SkRect& dst,
2113 const SkPaint* paint, DrawBitmapRectFlags flags) { 2128 const SkPaint* paint, DrawBitmapRectFlags flags) {
2114 TRACE_EVENT0("disabled-by-default-skia", "SkCanvas::drawBitmapRectToRect()") ; 2129 TRACE_EVENT0("disabled-by-default-skia", "SkCanvas::drawBitmapRectToRect()") ;
2115 SkDEBUGCODE(bitmap.validate();) 2130 SkDEBUGCODE(bitmap.validate();)
2116 this->internalDrawBitmapRect(bitmap, src, dst, paint, flags); 2131 this->internalDrawBitmapRect(bitmap, src, dst, paint, flags);
2117 } 2132 }
2118 2133
2119 void SkCanvas::internalDrawBitmapNine(const SkBitmap& bitmap, 2134 void SkCanvas::onDrawImageNine(const SkImage* image, const SkIRect& center, cons t SkRect& dst,
2120 const SkIRect& center, const SkRect& dst, 2135 const SkPaint* paint) {
2121 const SkPaint* paint) { 2136 TRACE_EVENT0("disabled-by-default-skia", "SkCanvas::drawImageNine()");
2122 if (bitmap.drawsNothing()) { 2137
2123 return; 2138 SkRect storage;
2124 } 2139 const SkRect* bounds = &dst;
2125 if (NULL == paint || paint->canComputeFastBounds()) { 2140 if (NULL == paint || paint->canComputeFastBounds()) {
2126 SkRect storage;
2127 const SkRect* bounds = &dst;
2128 if (paint) { 2141 if (paint) {
2129 bounds = &paint->computeFastBounds(dst, &storage); 2142 bounds = &paint->computeFastBounds(dst, &storage);
2130 } 2143 }
2131 if (this->quickReject(*bounds)) { 2144 if (this->quickReject(*bounds)) {
2132 return; 2145 return;
2133 } 2146 }
2134 } 2147 }
2135 2148
2136 const int32_t w = bitmap.width(); 2149 SkLazyPaint lazy;
2137 const int32_t h = bitmap.height(); 2150 if (NULL == paint) {
2138 2151 paint = lazy.init();
2139 SkIRect c = center;
2140 // pin center to the bounds of the bitmap
2141 c.fLeft = SkMax32(0, center.fLeft);
2142 c.fTop = SkMax32(0, center.fTop);
2143 c.fRight = SkPin32(center.fRight, c.fLeft, w);
2144 c.fBottom = SkPin32(center.fBottom, c.fTop, h);
2145
2146 const SkScalar srcX[4] = {
2147 0, SkIntToScalar(c.fLeft), SkIntToScalar(c.fRight), SkIntToScalar(w)
2148 };
2149 const SkScalar srcY[4] = {
2150 0, SkIntToScalar(c.fTop), SkIntToScalar(c.fBottom), SkIntToScalar(h)
2151 };
2152 SkScalar dstX[4] = {
2153 dst.fLeft, dst.fLeft + SkIntToScalar(c.fLeft),
2154 dst.fRight - SkIntToScalar(w - c.fRight), dst.fRight
2155 };
2156 SkScalar dstY[4] = {
2157 dst.fTop, dst.fTop + SkIntToScalar(c.fTop),
2158 dst.fBottom - SkIntToScalar(h - c.fBottom), dst.fBottom
2159 };
2160
2161 if (dstX[1] > dstX[2]) {
2162 dstX[1] = dstX[0] + (dstX[3] - dstX[0]) * c.fLeft / (w - c.width());
2163 dstX[2] = dstX[1];
2164 } 2152 }
2165 2153
2166 if (dstY[1] > dstY[2]) { 2154 LOOPER_BEGIN(*paint, SkDrawFilter::kBitmap_Type, bounds)
2167 dstY[1] = dstY[0] + (dstY[3] - dstY[0]) * c.fTop / (h - c.height()); 2155
2168 dstY[2] = dstY[1]; 2156 while (iter.next()) {
2157 iter.fDevice->drawImageNine(iter, image, center, dst, looper.paint());
2169 } 2158 }
2170 2159
2171 for (int y = 0; y < 3; y++) { 2160 LOOPER_END
2172 SkRect s, d;
2173
2174 s.fTop = srcY[y];
2175 s.fBottom = srcY[y+1];
2176 d.fTop = dstY[y];
2177 d.fBottom = dstY[y+1];
2178 for (int x = 0; x < 3; x++) {
2179 s.fLeft = srcX[x];
2180 s.fRight = srcX[x+1];
2181 d.fLeft = dstX[x];
2182 d.fRight = dstX[x+1];
2183 this->internalDrawBitmapRect(bitmap, &s, d, paint,
2184 kNone_DrawBitmapRectFlag);
2185 }
2186 }
2187 } 2161 }
2188 2162
2189 void SkCanvas::onDrawBitmapNine(const SkBitmap& bitmap, const SkIRect& center, c onst SkRect& dst, 2163 void SkCanvas::onDrawBitmapNine(const SkBitmap& bitmap, const SkIRect& center, c onst SkRect& dst,
2190 const SkPaint* paint) { 2164 const SkPaint* paint) {
2191 TRACE_EVENT0("disabled-by-default-skia", "SkCanvas::drawBitmapNine()"); 2165 TRACE_EVENT0("disabled-by-default-skia", "SkCanvas::drawBitmapNine()");
2192 SkDEBUGCODE(bitmap.validate();) 2166 SkDEBUGCODE(bitmap.validate();)
2193 2167
2194 // Need a device entry-point, so gpu can use a mesh 2168 SkRect storage;
2195 this->internalDrawBitmapNine(bitmap, center, dst, paint); 2169 const SkRect* bounds = &dst;
2170 if (NULL == paint || paint->canComputeFastBounds()) {
2171 if (paint) {
2172 bounds = &paint->computeFastBounds(dst, &storage);
2173 }
2174 if (this->quickReject(*bounds)) {
2175 return;
2176 }
2177 }
2178
2179 SkLazyPaint lazy;
2180 if (NULL == paint) {
2181 paint = lazy.init();
2182 }
2183
2184 LOOPER_BEGIN(*paint, SkDrawFilter::kBitmap_Type, bounds)
2185
2186 while (iter.next()) {
2187 iter.fDevice->drawBitmapNine(iter, bitmap, center, dst, looper.paint());
2188 }
2189
2190 LOOPER_END
2196 } 2191 }
2197 2192
2198 class SkDeviceFilteredPaint { 2193 class SkDeviceFilteredPaint {
2199 public: 2194 public:
2200 SkDeviceFilteredPaint(SkBaseDevice* device, const SkPaint& paint) { 2195 SkDeviceFilteredPaint(SkBaseDevice* device, const SkPaint& paint) {
2201 uint32_t filteredFlags = device->filterTextFlags(paint); 2196 uint32_t filteredFlags = device->filterTextFlags(paint);
2202 if (filteredFlags != paint.getFlags()) { 2197 if (filteredFlags != paint.getFlags()) {
2203 SkPaint* newPaint = fLazy.set(paint); 2198 SkPaint* newPaint = fLazy.set(paint);
2204 newPaint->setFlags(filteredFlags); 2199 newPaint->setFlags(filteredFlags);
2205 fPaint = newPaint; 2200 fPaint = newPaint;
(...skipping 537 matching lines...) Expand 10 before | Expand all | Expand 10 after
2743 } 2738 }
2744 2739
2745 if (matrix) { 2740 if (matrix) {
2746 canvas->concat(*matrix); 2741 canvas->concat(*matrix);
2747 } 2742 }
2748 } 2743 }
2749 2744
2750 SkAutoCanvasMatrixPaint::~SkAutoCanvasMatrixPaint() { 2745 SkAutoCanvasMatrixPaint::~SkAutoCanvasMatrixPaint() {
2751 fCanvas->restoreToCount(fSaveCount); 2746 fCanvas->restoreToCount(fSaveCount);
2752 } 2747 }
OLDNEW
« no previous file with comments | « include/utils/SkDeferredCanvas.h ('k') | src/core/SkDevice.cpp » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698