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

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

Issue 207683004: Extract most of the mutable state of SkShader into a separate Context object. (Closed) Base URL: https://skia.googlesource.com/skia.git@master
Patch Set: rebase & Leon's comments Created 6 years, 8 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
OLDNEW
1 1
2 /* 2 /*
3 * Copyright 2008 The Android Open Source Project 3 * Copyright 2008 The Android Open Source Project
4 * 4 *
5 * Use of this source code is governed by a BSD-style license that can be 5 * Use of this source code is governed by a BSD-style license that can be
6 * found in the LICENSE file. 6 * found in the LICENSE file.
7 */ 7 */
8 8
9 9
10 #include "SkCanvas.h" 10 #include "SkCanvas.h"
(...skipping 73 matching lines...) Expand 10 before | Expand all | Expand 10 after
84 const int count = fPixelRef ? fPixelRef->getLockCount() : 0; 84 const int count = fPixelRef ? fPixelRef->getLockCount() : 0;
85 SkASSERT(count == fLockCount); 85 SkASSERT(count == fLockCount);
86 } 86 }
87 87
88 private: 88 private:
89 const SkPixelRef* fPixelRef; 89 const SkPixelRef* fPixelRef;
90 int fLockCount; 90 int fLockCount;
91 }; 91 };
92 #endif 92 #endif
93 93
94 class AutoCheckNoSetContext {
95 public:
96 AutoCheckNoSetContext(const SkPaint& paint) : fPaint(paint) {
97 this->assertNoSetContext(fPaint);
98 }
99 ~AutoCheckNoSetContext() {
100 this->assertNoSetContext(fPaint);
101 }
102
103 private:
104 const SkPaint& fPaint;
105
106 void assertNoSetContext(const SkPaint& paint) {
107 SkShader* s = paint.getShader();
108 if (s) {
109 SkASSERT(!s->setContextHasBeenCalled());
110 }
111 }
112 };
113
114 #define CHECK_LOCKCOUNT_BALANCE(bitmap) AutoCheckLockCountBalance clcb(bitmap) 94 #define CHECK_LOCKCOUNT_BALANCE(bitmap) AutoCheckLockCountBalance clcb(bitmap)
115 #define CHECK_SHADER_NOSETCONTEXT(paint) AutoCheckNoSetContext cshsc(paint)
116 95
117 #else 96 #else
118 #define CHECK_LOCKCOUNT_BALANCE(bitmap) 97 #define CHECK_LOCKCOUNT_BALANCE(bitmap)
119 #define CHECK_SHADER_NOSETCONTEXT(paint)
120 #endif 98 #endif
121 99
122 typedef SkTLazy<SkPaint> SkLazyPaint; 100 typedef SkTLazy<SkPaint> SkLazyPaint;
123 101
124 void SkCanvas::predrawNotify() { 102 void SkCanvas::predrawNotify() {
125 if (fSurfaceBase) { 103 if (fSurfaceBase) {
126 fSurfaceBase->aboutToDraw(SkSurface::kRetain_ContentChangeMode); 104 fSurfaceBase->aboutToDraw(SkSurface::kRetain_ContentChangeMode);
127 } 105 }
128 } 106 }
129 107
(...skipping 1798 matching lines...) Expand 10 before | Expand all | Expand 10 after
1928 if (NULL != fSurfaceBase) { 1906 if (NULL != fSurfaceBase) {
1929 fSurfaceBase->aboutToDraw(SkSurface::kDiscard_ContentChangeMode); 1907 fSurfaceBase->aboutToDraw(SkSurface::kDiscard_ContentChangeMode);
1930 } 1908 }
1931 } 1909 }
1932 1910
1933 void SkCanvas::drawPaint(const SkPaint& paint) { 1911 void SkCanvas::drawPaint(const SkPaint& paint) {
1934 this->internalDrawPaint(paint); 1912 this->internalDrawPaint(paint);
1935 } 1913 }
1936 1914
1937 void SkCanvas::internalDrawPaint(const SkPaint& paint) { 1915 void SkCanvas::internalDrawPaint(const SkPaint& paint) {
1938 CHECK_SHADER_NOSETCONTEXT(paint);
1939
1940 LOOPER_BEGIN(paint, SkDrawFilter::kPaint_Type, NULL) 1916 LOOPER_BEGIN(paint, SkDrawFilter::kPaint_Type, NULL)
1941 1917
1942 while (iter.next()) { 1918 while (iter.next()) {
1943 iter.fDevice->drawPaint(iter, looper.paint()); 1919 iter.fDevice->drawPaint(iter, looper.paint());
1944 } 1920 }
1945 1921
1946 LOOPER_END 1922 LOOPER_END
1947 } 1923 }
1948 1924
1949 void SkCanvas::drawPoints(PointMode mode, size_t count, const SkPoint pts[], 1925 void SkCanvas::drawPoints(PointMode mode, size_t count, const SkPoint pts[],
1950 const SkPaint& paint) { 1926 const SkPaint& paint) {
1951 if ((long)count <= 0) { 1927 if ((long)count <= 0) {
1952 return; 1928 return;
1953 } 1929 }
1954 1930
1955 CHECK_SHADER_NOSETCONTEXT(paint);
1956
1957 SkRect r, storage; 1931 SkRect r, storage;
1958 const SkRect* bounds = NULL; 1932 const SkRect* bounds = NULL;
1959 if (paint.canComputeFastBounds()) { 1933 if (paint.canComputeFastBounds()) {
1960 // special-case 2 points (common for drawing a single line) 1934 // special-case 2 points (common for drawing a single line)
1961 if (2 == count) { 1935 if (2 == count) {
1962 r.set(pts[0], pts[1]); 1936 r.set(pts[0], pts[1]);
1963 } else { 1937 } else {
1964 r.set(pts, SkToInt(count)); 1938 r.set(pts, SkToInt(count));
1965 } 1939 }
1966 bounds = &paint.computeFastStrokeBounds(r, &storage); 1940 bounds = &paint.computeFastStrokeBounds(r, &storage);
1967 if (this->quickReject(*bounds)) { 1941 if (this->quickReject(*bounds)) {
1968 return; 1942 return;
1969 } 1943 }
1970 } 1944 }
1971 1945
1972 SkASSERT(pts != NULL); 1946 SkASSERT(pts != NULL);
1973 1947
1974 LOOPER_BEGIN(paint, SkDrawFilter::kPoint_Type, bounds) 1948 LOOPER_BEGIN(paint, SkDrawFilter::kPoint_Type, bounds)
1975 1949
1976 while (iter.next()) { 1950 while (iter.next()) {
1977 iter.fDevice->drawPoints(iter, mode, count, pts, looper.paint()); 1951 iter.fDevice->drawPoints(iter, mode, count, pts, looper.paint());
1978 } 1952 }
1979 1953
1980 LOOPER_END 1954 LOOPER_END
1981 } 1955 }
1982 1956
1983 void SkCanvas::drawRect(const SkRect& r, const SkPaint& paint) { 1957 void SkCanvas::drawRect(const SkRect& r, const SkPaint& paint) {
1984 CHECK_SHADER_NOSETCONTEXT(paint);
1985
1986 SkRect storage; 1958 SkRect storage;
1987 const SkRect* bounds = NULL; 1959 const SkRect* bounds = NULL;
1988 if (paint.canComputeFastBounds()) { 1960 if (paint.canComputeFastBounds()) {
1989 bounds = &paint.computeFastBounds(r, &storage); 1961 bounds = &paint.computeFastBounds(r, &storage);
1990 if (this->quickReject(*bounds)) { 1962 if (this->quickReject(*bounds)) {
1991 return; 1963 return;
1992 } 1964 }
1993 } 1965 }
1994 1966
1995 LOOPER_BEGIN(paint, SkDrawFilter::kRect_Type, bounds) 1967 LOOPER_BEGIN(paint, SkDrawFilter::kRect_Type, bounds)
1996 1968
1997 while (iter.next()) { 1969 while (iter.next()) {
1998 iter.fDevice->drawRect(iter, r, looper.paint()); 1970 iter.fDevice->drawRect(iter, r, looper.paint());
1999 } 1971 }
2000 1972
2001 LOOPER_END 1973 LOOPER_END
2002 } 1974 }
2003 1975
2004 void SkCanvas::drawOval(const SkRect& oval, const SkPaint& paint) { 1976 void SkCanvas::drawOval(const SkRect& oval, const SkPaint& paint) {
2005 CHECK_SHADER_NOSETCONTEXT(paint);
2006
2007 SkRect storage; 1977 SkRect storage;
2008 const SkRect* bounds = NULL; 1978 const SkRect* bounds = NULL;
2009 if (paint.canComputeFastBounds()) { 1979 if (paint.canComputeFastBounds()) {
2010 bounds = &paint.computeFastBounds(oval, &storage); 1980 bounds = &paint.computeFastBounds(oval, &storage);
2011 if (this->quickReject(*bounds)) { 1981 if (this->quickReject(*bounds)) {
2012 return; 1982 return;
2013 } 1983 }
2014 } 1984 }
2015 1985
2016 LOOPER_BEGIN(paint, SkDrawFilter::kOval_Type, bounds) 1986 LOOPER_BEGIN(paint, SkDrawFilter::kOval_Type, bounds)
2017 1987
2018 while (iter.next()) { 1988 while (iter.next()) {
2019 iter.fDevice->drawOval(iter, oval, looper.paint()); 1989 iter.fDevice->drawOval(iter, oval, looper.paint());
2020 } 1990 }
2021 1991
2022 LOOPER_END 1992 LOOPER_END
2023 } 1993 }
2024 1994
2025 void SkCanvas::drawRRect(const SkRRect& rrect, const SkPaint& paint) { 1995 void SkCanvas::drawRRect(const SkRRect& rrect, const SkPaint& paint) {
2026 CHECK_SHADER_NOSETCONTEXT(paint);
2027
2028 SkRect storage; 1996 SkRect storage;
2029 const SkRect* bounds = NULL; 1997 const SkRect* bounds = NULL;
2030 if (paint.canComputeFastBounds()) { 1998 if (paint.canComputeFastBounds()) {
2031 bounds = &paint.computeFastBounds(rrect.getBounds(), &storage); 1999 bounds = &paint.computeFastBounds(rrect.getBounds(), &storage);
2032 if (this->quickReject(*bounds)) { 2000 if (this->quickReject(*bounds)) {
2033 return; 2001 return;
2034 } 2002 }
2035 } 2003 }
2036 2004
2037 if (rrect.isRect()) { 2005 if (rrect.isRect()) {
(...skipping 10 matching lines...) Expand all
2048 2016
2049 while (iter.next()) { 2017 while (iter.next()) {
2050 iter.fDevice->drawRRect(iter, rrect, looper.paint()); 2018 iter.fDevice->drawRRect(iter, rrect, looper.paint());
2051 } 2019 }
2052 2020
2053 LOOPER_END 2021 LOOPER_END
2054 } 2022 }
2055 2023
2056 void SkCanvas::onDrawDRRect(const SkRRect& outer, const SkRRect& inner, 2024 void SkCanvas::onDrawDRRect(const SkRRect& outer, const SkRRect& inner,
2057 const SkPaint& paint) { 2025 const SkPaint& paint) {
2058 CHECK_SHADER_NOSETCONTEXT(paint);
2059
2060 SkRect storage; 2026 SkRect storage;
2061 const SkRect* bounds = NULL; 2027 const SkRect* bounds = NULL;
2062 if (paint.canComputeFastBounds()) { 2028 if (paint.canComputeFastBounds()) {
2063 bounds = &paint.computeFastBounds(outer.getBounds(), &storage); 2029 bounds = &paint.computeFastBounds(outer.getBounds(), &storage);
2064 if (this->quickReject(*bounds)) { 2030 if (this->quickReject(*bounds)) {
2065 return; 2031 return;
2066 } 2032 }
2067 } 2033 }
2068 2034
2069 LOOPER_BEGIN(paint, SkDrawFilter::kRRect_Type, bounds) 2035 LOOPER_BEGIN(paint, SkDrawFilter::kRRect_Type, bounds)
2070 2036
2071 while (iter.next()) { 2037 while (iter.next()) {
2072 iter.fDevice->drawDRRect(iter, outer, inner, looper.paint()); 2038 iter.fDevice->drawDRRect(iter, outer, inner, looper.paint());
2073 } 2039 }
2074 2040
2075 LOOPER_END 2041 LOOPER_END
2076 } 2042 }
2077 2043
2078 void SkCanvas::drawPath(const SkPath& path, const SkPaint& paint) { 2044 void SkCanvas::drawPath(const SkPath& path, const SkPaint& paint) {
2079 CHECK_SHADER_NOSETCONTEXT(paint);
2080
2081 if (!path.isFinite()) { 2045 if (!path.isFinite()) {
2082 return; 2046 return;
2083 } 2047 }
2084 2048
2085 SkRect storage; 2049 SkRect storage;
2086 const SkRect* bounds = NULL; 2050 const SkRect* bounds = NULL;
2087 if (!path.isInverseFillType() && paint.canComputeFastBounds()) { 2051 if (!path.isInverseFillType() && paint.canComputeFastBounds()) {
2088 const SkRect& pathBounds = path.getBounds(); 2052 const SkRect& pathBounds = path.getBounds();
2089 bounds = &paint.computeFastBounds(pathBounds, &storage); 2053 bounds = &paint.computeFastBounds(pathBounds, &storage);
2090 if (this->quickReject(*bounds)) { 2054 if (this->quickReject(*bounds)) {
(...skipping 255 matching lines...) Expand 10 before | Expand all | Expand 10 after
2346 start.fY); 2310 start.fY);
2347 r.fTop = offset; 2311 r.fTop = offset;
2348 r.fBottom = offset + height; 2312 r.fBottom = offset + height;
2349 DrawRect(draw, paint, r, textSize); 2313 DrawRect(draw, paint, r, textSize);
2350 } 2314 }
2351 } 2315 }
2352 } 2316 }
2353 2317
2354 void SkCanvas::drawText(const void* text, size_t byteLength, 2318 void SkCanvas::drawText(const void* text, size_t byteLength,
2355 SkScalar x, SkScalar y, const SkPaint& paint) { 2319 SkScalar x, SkScalar y, const SkPaint& paint) {
2356 CHECK_SHADER_NOSETCONTEXT(paint);
2357
2358 LOOPER_BEGIN(paint, SkDrawFilter::kText_Type, NULL) 2320 LOOPER_BEGIN(paint, SkDrawFilter::kText_Type, NULL)
2359 2321
2360 while (iter.next()) { 2322 while (iter.next()) {
2361 SkDeviceFilteredPaint dfp(iter.fDevice, looper.paint()); 2323 SkDeviceFilteredPaint dfp(iter.fDevice, looper.paint());
2362 iter.fDevice->drawText(iter, text, byteLength, x, y, dfp.paint()); 2324 iter.fDevice->drawText(iter, text, byteLength, x, y, dfp.paint());
2363 DrawTextDecorations(iter, dfp.paint(), 2325 DrawTextDecorations(iter, dfp.paint(),
2364 static_cast<const char*>(text), byteLength, x, y); 2326 static_cast<const char*>(text), byteLength, x, y);
2365 } 2327 }
2366 2328
2367 LOOPER_END 2329 LOOPER_END
2368 } 2330 }
2369 2331
2370 void SkCanvas::drawPosText(const void* text, size_t byteLength, 2332 void SkCanvas::drawPosText(const void* text, size_t byteLength,
2371 const SkPoint pos[], const SkPaint& paint) { 2333 const SkPoint pos[], const SkPaint& paint) {
2372 CHECK_SHADER_NOSETCONTEXT(paint);
2373
2374 LOOPER_BEGIN(paint, SkDrawFilter::kText_Type, NULL) 2334 LOOPER_BEGIN(paint, SkDrawFilter::kText_Type, NULL)
2375 2335
2376 while (iter.next()) { 2336 while (iter.next()) {
2377 SkDeviceFilteredPaint dfp(iter.fDevice, looper.paint()); 2337 SkDeviceFilteredPaint dfp(iter.fDevice, looper.paint());
2378 iter.fDevice->drawPosText(iter, text, byteLength, &pos->fX, 0, 2, 2338 iter.fDevice->drawPosText(iter, text, byteLength, &pos->fX, 0, 2,
2379 dfp.paint()); 2339 dfp.paint());
2380 } 2340 }
2381 2341
2382 LOOPER_END 2342 LOOPER_END
2383 } 2343 }
2384 2344
2385 void SkCanvas::drawPosTextH(const void* text, size_t byteLength, 2345 void SkCanvas::drawPosTextH(const void* text, size_t byteLength,
2386 const SkScalar xpos[], SkScalar constY, 2346 const SkScalar xpos[], SkScalar constY,
2387 const SkPaint& paint) { 2347 const SkPaint& paint) {
2388 CHECK_SHADER_NOSETCONTEXT(paint);
2389
2390 LOOPER_BEGIN(paint, SkDrawFilter::kText_Type, NULL) 2348 LOOPER_BEGIN(paint, SkDrawFilter::kText_Type, NULL)
2391 2349
2392 while (iter.next()) { 2350 while (iter.next()) {
2393 SkDeviceFilteredPaint dfp(iter.fDevice, looper.paint()); 2351 SkDeviceFilteredPaint dfp(iter.fDevice, looper.paint());
2394 iter.fDevice->drawPosText(iter, text, byteLength, xpos, constY, 1, 2352 iter.fDevice->drawPosText(iter, text, byteLength, xpos, constY, 1,
2395 dfp.paint()); 2353 dfp.paint());
2396 } 2354 }
2397 2355
2398 LOOPER_END 2356 LOOPER_END
2399 } 2357 }
2400 2358
2401 void SkCanvas::drawTextOnPath(const void* text, size_t byteLength, 2359 void SkCanvas::drawTextOnPath(const void* text, size_t byteLength,
2402 const SkPath& path, const SkMatrix* matrix, 2360 const SkPath& path, const SkMatrix* matrix,
2403 const SkPaint& paint) { 2361 const SkPaint& paint) {
2404 CHECK_SHADER_NOSETCONTEXT(paint);
2405
2406 LOOPER_BEGIN(paint, SkDrawFilter::kText_Type, NULL) 2362 LOOPER_BEGIN(paint, SkDrawFilter::kText_Type, NULL)
2407 2363
2408 while (iter.next()) { 2364 while (iter.next()) {
2409 iter.fDevice->drawTextOnPath(iter, text, byteLength, path, 2365 iter.fDevice->drawTextOnPath(iter, text, byteLength, path,
2410 matrix, looper.paint()); 2366 matrix, looper.paint());
2411 } 2367 }
2412 2368
2413 LOOPER_END 2369 LOOPER_END
2414 } 2370 }
2415 2371
2416 void SkCanvas::drawVertices(VertexMode vmode, int vertexCount, 2372 void SkCanvas::drawVertices(VertexMode vmode, int vertexCount,
2417 const SkPoint verts[], const SkPoint texs[], 2373 const SkPoint verts[], const SkPoint texs[],
2418 const SkColor colors[], SkXfermode* xmode, 2374 const SkColor colors[], SkXfermode* xmode,
2419 const uint16_t indices[], int indexCount, 2375 const uint16_t indices[], int indexCount,
2420 const SkPaint& paint) { 2376 const SkPaint& paint) {
2421 CHECK_SHADER_NOSETCONTEXT(paint);
2422
2423 LOOPER_BEGIN(paint, SkDrawFilter::kPath_Type, NULL) 2377 LOOPER_BEGIN(paint, SkDrawFilter::kPath_Type, NULL)
2424 2378
2425 while (iter.next()) { 2379 while (iter.next()) {
2426 iter.fDevice->drawVertices(iter, vmode, vertexCount, verts, texs, 2380 iter.fDevice->drawVertices(iter, vmode, vertexCount, verts, texs,
2427 colors, xmode, indices, indexCount, 2381 colors, xmode, indices, indexCount,
2428 looper.paint()); 2382 looper.paint());
2429 } 2383 }
2430 2384
2431 LOOPER_END 2385 LOOPER_END
2432 } 2386 }
(...skipping 234 matching lines...) Expand 10 before | Expand all | Expand 10 after
2667 if (!bitmap.installPixels(info, pixels, rowBytes)) { 2621 if (!bitmap.installPixels(info, pixels, rowBytes)) {
2668 return NULL; 2622 return NULL;
2669 } 2623 }
2670 2624
2671 // should this functionality be moved into allocPixels()? 2625 // should this functionality be moved into allocPixels()?
2672 if (!bitmap.info().isOpaque()) { 2626 if (!bitmap.info().isOpaque()) {
2673 bitmap.eraseColor(0); 2627 bitmap.eraseColor(0);
2674 } 2628 }
2675 return SkNEW_ARGS(SkCanvas, (bitmap)); 2629 return SkNEW_ARGS(SkCanvas, (bitmap));
2676 } 2630 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698