scroggo@google.com | d614c6a | 2012-09-14 17:26:37 +0000 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2012 Google Inc. |
| 3 | * |
| 4 | * Use of this source code is governed by a BSD-style license that can be |
| 5 | * found in the LICENSE file. |
| 6 | */ |
tfarina@chromium.org | e4fafb1 | 2013-12-12 21:11:12 +0000 | [diff] [blame] | 7 | |
commit-bot@chromium.org | 0205aba | 2014-05-06 12:02:22 +0000 | [diff] [blame] | 8 | #include "SkBlurImageFilter.h" |
reed@google.com | 21b519d | 2012-10-02 17:42:15 +0000 | [diff] [blame] | 9 | #include "SkCanvas.h" |
reed@google.com | fe7b1ed | 2012-11-29 21:00:39 +0000 | [diff] [blame] | 10 | #include "SkColorPriv.h" |
commit-bot@chromium.org | e2cb12a | 2014-04-24 21:53:13 +0000 | [diff] [blame] | 11 | #include "SkDashPathEffect.h" |
reed@google.com | fe7b1ed | 2012-11-29 21:00:39 +0000 | [diff] [blame] | 12 | #include "SkData.h" |
reed@google.com | bf79023 | 2013-12-13 19:45:58 +0000 | [diff] [blame] | 13 | #include "SkDecodingImageGenerator.h" |
scroggo@google.com | 49ce11b | 2013-04-25 18:29:32 +0000 | [diff] [blame] | 14 | #include "SkError.h" |
halcanary@google.com | 3d50ea1 | 2014-01-02 13:15:13 +0000 | [diff] [blame] | 15 | #include "SkImageEncoder.h" |
| 16 | #include "SkImageGenerator.h" |
reed@google.com | 21b519d | 2012-10-02 17:42:15 +0000 | [diff] [blame] | 17 | #include "SkPaint.h" |
scroggo@google.com | d614c6a | 2012-09-14 17:26:37 +0000 | [diff] [blame] | 18 | #include "SkPicture.h" |
robertphillips@google.com | 770963f | 2014-04-18 18:04:41 +0000 | [diff] [blame] | 19 | #include "SkPictureRecorder.h" |
robertphillips@google.com | 1f2f338 | 2013-08-29 11:54:56 +0000 | [diff] [blame] | 20 | #include "SkPictureUtils.h" |
reed@google.com | 72aa79c | 2013-01-24 18:27:42 +0000 | [diff] [blame] | 21 | #include "SkRRect.h" |
tfarina@chromium.org | 8f6884a | 2014-01-24 20:56:26 +0000 | [diff] [blame] | 22 | #include "SkRandom.h" |
reed@google.com | fe7b1ed | 2012-11-29 21:00:39 +0000 | [diff] [blame] | 23 | #include "SkShader.h" |
scroggo@google.com | d614c6a | 2012-09-14 17:26:37 +0000 | [diff] [blame] | 24 | #include "SkStream.h" |
commit-bot@chromium.org | 0205aba | 2014-05-06 12:02:22 +0000 | [diff] [blame] | 25 | |
| 26 | #if SK_SUPPORT_GPU |
| 27 | #include "SkSurface.h" |
| 28 | #include "GrContextFactory.h" |
| 29 | #include "GrPictureUtils.h" |
| 30 | #endif |
tfarina@chromium.org | 8f6884a | 2014-01-24 20:56:26 +0000 | [diff] [blame] | 31 | #include "Test.h" |
scroggo@google.com | d614c6a | 2012-09-14 17:26:37 +0000 | [diff] [blame] | 32 | |
reed@google.com | 47b679b | 2014-05-14 18:58:16 +0000 | [diff] [blame] | 33 | #include "SkLumaColorFilter.h" |
| 34 | #include "SkColorFilterImageFilter.h" |
| 35 | |
robertphillips@google.com | ed9866c | 2014-01-09 19:20:45 +0000 | [diff] [blame] | 36 | static const int gColorScale = 30; |
| 37 | static const int gColorOffset = 60; |
reed@google.com | fe7b1ed | 2012-11-29 21:00:39 +0000 | [diff] [blame] | 38 | |
| 39 | static void make_bm(SkBitmap* bm, int w, int h, SkColor color, bool immutable) { |
mike@reedtribe.org | deee496 | 2014-02-13 14:41:43 +0000 | [diff] [blame] | 40 | bm->allocN32Pixels(w, h); |
reed@google.com | fe7b1ed | 2012-11-29 21:00:39 +0000 | [diff] [blame] | 41 | bm->eraseColor(color); |
| 42 | if (immutable) { |
| 43 | bm->setImmutable(); |
| 44 | } |
| 45 | } |
| 46 | |
robertphillips@google.com | fe5824a | 2014-01-09 19:45:29 +0000 | [diff] [blame] | 47 | static void make_checkerboard(SkBitmap* bm, int w, int h, bool immutable) { |
robertphillips@google.com | ed9866c | 2014-01-09 19:20:45 +0000 | [diff] [blame] | 48 | SkASSERT(w % 2 == 0); |
| 49 | SkASSERT(h % 2 == 0); |
mike@reedtribe.org | deee496 | 2014-02-13 14:41:43 +0000 | [diff] [blame] | 50 | bm->allocPixels(SkImageInfo::Make(w, h, kAlpha_8_SkColorType, |
| 51 | kPremul_SkAlphaType)); |
robertphillips@google.com | ed9866c | 2014-01-09 19:20:45 +0000 | [diff] [blame] | 52 | SkAutoLockPixels lock(*bm); |
| 53 | for (int y = 0; y < h; y += 2) { |
| 54 | uint8_t* s = bm->getAddr8(0, y); |
| 55 | for (int x = 0; x < w; x += 2) { |
| 56 | *s++ = 0xFF; |
| 57 | *s++ = 0x00; |
| 58 | } |
| 59 | s = bm->getAddr8(0, y + 1); |
| 60 | for (int x = 0; x < w; x += 2) { |
| 61 | *s++ = 0x00; |
| 62 | *s++ = 0xFF; |
| 63 | } |
| 64 | } |
| 65 | if (immutable) { |
| 66 | bm->setImmutable(); |
| 67 | } |
| 68 | } |
reed@google.com | fe7b1ed | 2012-11-29 21:00:39 +0000 | [diff] [blame] | 69 | |
robertphillips@google.com | ed9866c | 2014-01-09 19:20:45 +0000 | [diff] [blame] | 70 | static void init_paint(SkPaint* paint, const SkBitmap &bm) { |
| 71 | SkShader* shader = SkShader::CreateBitmapShader(bm, |
| 72 | SkShader::kClamp_TileMode, |
| 73 | SkShader::kClamp_TileMode); |
| 74 | paint->setShader(shader)->unref(); |
| 75 | } |
| 76 | |
skia.committer@gmail.com | 2e9a715 | 2014-01-14 07:01:42 +0000 | [diff] [blame] | 77 | typedef void (*DrawBitmapProc)(SkCanvas*, const SkBitmap&, |
robertphillips@google.com | 56bf6e4 | 2014-01-13 13:33:26 +0000 | [diff] [blame] | 78 | const SkBitmap&, const SkPoint&, |
| 79 | SkTDArray<SkPixelRef*>* usedPixRefs); |
robertphillips@google.com | ed9866c | 2014-01-09 19:20:45 +0000 | [diff] [blame] | 80 | |
skia.committer@gmail.com | 856673a | 2014-01-10 07:08:11 +0000 | [diff] [blame] | 81 | static void drawpaint_proc(SkCanvas* canvas, const SkBitmap& bm, |
robertphillips@google.com | 56bf6e4 | 2014-01-13 13:33:26 +0000 | [diff] [blame] | 82 | const SkBitmap& altBM, const SkPoint& pos, |
| 83 | SkTDArray<SkPixelRef*>* usedPixRefs) { |
robertphillips@google.com | ed9866c | 2014-01-09 19:20:45 +0000 | [diff] [blame] | 84 | SkPaint paint; |
| 85 | init_paint(&paint, bm); |
| 86 | |
| 87 | canvas->drawPaint(paint); |
robertphillips@google.com | 56bf6e4 | 2014-01-13 13:33:26 +0000 | [diff] [blame] | 88 | *usedPixRefs->append() = bm.pixelRef(); |
robertphillips@google.com | ed9866c | 2014-01-09 19:20:45 +0000 | [diff] [blame] | 89 | } |
| 90 | |
skia.committer@gmail.com | 856673a | 2014-01-10 07:08:11 +0000 | [diff] [blame] | 91 | static void drawpoints_proc(SkCanvas* canvas, const SkBitmap& bm, |
robertphillips@google.com | 56bf6e4 | 2014-01-13 13:33:26 +0000 | [diff] [blame] | 92 | const SkBitmap& altBM, const SkPoint& pos, |
| 93 | SkTDArray<SkPixelRef*>* usedPixRefs) { |
robertphillips@google.com | ed9866c | 2014-01-09 19:20:45 +0000 | [diff] [blame] | 94 | SkPaint paint; |
| 95 | init_paint(&paint, bm); |
| 96 | |
robertphillips@google.com | 56bf6e4 | 2014-01-13 13:33:26 +0000 | [diff] [blame] | 97 | // draw a rect |
robertphillips@google.com | ed9866c | 2014-01-09 19:20:45 +0000 | [diff] [blame] | 98 | SkPoint points[5] = { |
skia.committer@gmail.com | 2e9a715 | 2014-01-14 07:01:42 +0000 | [diff] [blame] | 99 | { pos.fX, pos.fY }, |
| 100 | { pos.fX + bm.width() - 1, pos.fY }, |
| 101 | { pos.fX + bm.width() - 1, pos.fY + bm.height() - 1 }, |
| 102 | { pos.fX, pos.fY + bm.height() - 1 }, |
| 103 | { pos.fX, pos.fY }, |
robertphillips@google.com | ed9866c | 2014-01-09 19:20:45 +0000 | [diff] [blame] | 104 | }; |
| 105 | |
| 106 | canvas->drawPoints(SkCanvas::kPolygon_PointMode, 5, points, paint); |
robertphillips@google.com | 56bf6e4 | 2014-01-13 13:33:26 +0000 | [diff] [blame] | 107 | *usedPixRefs->append() = bm.pixelRef(); |
robertphillips@google.com | ed9866c | 2014-01-09 19:20:45 +0000 | [diff] [blame] | 108 | } |
| 109 | |
skia.committer@gmail.com | 856673a | 2014-01-10 07:08:11 +0000 | [diff] [blame] | 110 | static void drawrect_proc(SkCanvas* canvas, const SkBitmap& bm, |
robertphillips@google.com | 56bf6e4 | 2014-01-13 13:33:26 +0000 | [diff] [blame] | 111 | const SkBitmap& altBM, const SkPoint& pos, |
| 112 | SkTDArray<SkPixelRef*>* usedPixRefs) { |
robertphillips@google.com | ed9866c | 2014-01-09 19:20:45 +0000 | [diff] [blame] | 113 | SkPaint paint; |
| 114 | init_paint(&paint, bm); |
| 115 | |
| 116 | SkRect r = { 0, 0, SkIntToScalar(bm.width()), SkIntToScalar(bm.height()) }; |
| 117 | r.offset(pos.fX, pos.fY); |
| 118 | |
| 119 | canvas->drawRect(r, paint); |
robertphillips@google.com | 56bf6e4 | 2014-01-13 13:33:26 +0000 | [diff] [blame] | 120 | *usedPixRefs->append() = bm.pixelRef(); |
robertphillips@google.com | ed9866c | 2014-01-09 19:20:45 +0000 | [diff] [blame] | 121 | } |
| 122 | |
skia.committer@gmail.com | 856673a | 2014-01-10 07:08:11 +0000 | [diff] [blame] | 123 | static void drawoval_proc(SkCanvas* canvas, const SkBitmap& bm, |
robertphillips@google.com | 56bf6e4 | 2014-01-13 13:33:26 +0000 | [diff] [blame] | 124 | const SkBitmap& altBM, const SkPoint& pos, |
| 125 | SkTDArray<SkPixelRef*>* usedPixRefs) { |
robertphillips@google.com | ed9866c | 2014-01-09 19:20:45 +0000 | [diff] [blame] | 126 | SkPaint paint; |
| 127 | init_paint(&paint, bm); |
| 128 | |
| 129 | SkRect r = { 0, 0, SkIntToScalar(bm.width()), SkIntToScalar(bm.height()) }; |
| 130 | r.offset(pos.fX, pos.fY); |
| 131 | |
| 132 | canvas->drawOval(r, paint); |
robertphillips@google.com | 56bf6e4 | 2014-01-13 13:33:26 +0000 | [diff] [blame] | 133 | *usedPixRefs->append() = bm.pixelRef(); |
robertphillips@google.com | ed9866c | 2014-01-09 19:20:45 +0000 | [diff] [blame] | 134 | } |
| 135 | |
skia.committer@gmail.com | 856673a | 2014-01-10 07:08:11 +0000 | [diff] [blame] | 136 | static void drawrrect_proc(SkCanvas* canvas, const SkBitmap& bm, |
robertphillips@google.com | 56bf6e4 | 2014-01-13 13:33:26 +0000 | [diff] [blame] | 137 | const SkBitmap& altBM, const SkPoint& pos, |
| 138 | SkTDArray<SkPixelRef*>* usedPixRefs) { |
robertphillips@google.com | ed9866c | 2014-01-09 19:20:45 +0000 | [diff] [blame] | 139 | SkPaint paint; |
| 140 | init_paint(&paint, bm); |
skia.committer@gmail.com | 856673a | 2014-01-10 07:08:11 +0000 | [diff] [blame] | 141 | |
robertphillips@google.com | ed9866c | 2014-01-09 19:20:45 +0000 | [diff] [blame] | 142 | SkRect r = { 0, 0, SkIntToScalar(bm.width()), SkIntToScalar(bm.height()) }; |
| 143 | r.offset(pos.fX, pos.fY); |
| 144 | |
| 145 | SkRRect rr; |
| 146 | rr.setRectXY(r, SkIntToScalar(bm.width())/4, SkIntToScalar(bm.height())/4); |
| 147 | canvas->drawRRect(rr, paint); |
robertphillips@google.com | 56bf6e4 | 2014-01-13 13:33:26 +0000 | [diff] [blame] | 148 | *usedPixRefs->append() = bm.pixelRef(); |
robertphillips@google.com | ed9866c | 2014-01-09 19:20:45 +0000 | [diff] [blame] | 149 | } |
| 150 | |
skia.committer@gmail.com | 856673a | 2014-01-10 07:08:11 +0000 | [diff] [blame] | 151 | static void drawpath_proc(SkCanvas* canvas, const SkBitmap& bm, |
robertphillips@google.com | 56bf6e4 | 2014-01-13 13:33:26 +0000 | [diff] [blame] | 152 | const SkBitmap& altBM, const SkPoint& pos, |
| 153 | SkTDArray<SkPixelRef*>* usedPixRefs) { |
robertphillips@google.com | ed9866c | 2014-01-09 19:20:45 +0000 | [diff] [blame] | 154 | SkPaint paint; |
| 155 | init_paint(&paint, bm); |
| 156 | |
| 157 | SkPath path; |
| 158 | path.lineTo(bm.width()/2.0f, SkIntToScalar(bm.height())); |
| 159 | path.lineTo(SkIntToScalar(bm.width()), 0); |
| 160 | path.close(); |
| 161 | path.offset(pos.fX, pos.fY); |
| 162 | |
| 163 | canvas->drawPath(path, paint); |
robertphillips@google.com | 56bf6e4 | 2014-01-13 13:33:26 +0000 | [diff] [blame] | 164 | *usedPixRefs->append() = bm.pixelRef(); |
robertphillips@google.com | ed9866c | 2014-01-09 19:20:45 +0000 | [diff] [blame] | 165 | } |
| 166 | |
skia.committer@gmail.com | 856673a | 2014-01-10 07:08:11 +0000 | [diff] [blame] | 167 | static void drawbitmap_proc(SkCanvas* canvas, const SkBitmap& bm, |
robertphillips@google.com | 56bf6e4 | 2014-01-13 13:33:26 +0000 | [diff] [blame] | 168 | const SkBitmap& altBM, const SkPoint& pos, |
| 169 | SkTDArray<SkPixelRef*>* usedPixRefs) { |
reed@google.com | fe7b1ed | 2012-11-29 21:00:39 +0000 | [diff] [blame] | 170 | canvas->drawBitmap(bm, pos.fX, pos.fY, NULL); |
robertphillips@google.com | 56bf6e4 | 2014-01-13 13:33:26 +0000 | [diff] [blame] | 171 | *usedPixRefs->append() = bm.pixelRef(); |
reed@google.com | fe7b1ed | 2012-11-29 21:00:39 +0000 | [diff] [blame] | 172 | } |
| 173 | |
skia.committer@gmail.com | 856673a | 2014-01-10 07:08:11 +0000 | [diff] [blame] | 174 | static void drawbitmap_withshader_proc(SkCanvas* canvas, const SkBitmap& bm, |
robertphillips@google.com | 56bf6e4 | 2014-01-13 13:33:26 +0000 | [diff] [blame] | 175 | const SkBitmap& altBM, const SkPoint& pos, |
| 176 | SkTDArray<SkPixelRef*>* usedPixRefs) { |
robertphillips@google.com | ed9866c | 2014-01-09 19:20:45 +0000 | [diff] [blame] | 177 | SkPaint paint; |
| 178 | init_paint(&paint, bm); |
| 179 | |
| 180 | // The bitmap in the paint is ignored unless we're drawing an A8 bitmap |
| 181 | canvas->drawBitmap(altBM, pos.fX, pos.fY, &paint); |
robertphillips@google.com | 56bf6e4 | 2014-01-13 13:33:26 +0000 | [diff] [blame] | 182 | *usedPixRefs->append() = bm.pixelRef(); |
| 183 | *usedPixRefs->append() = altBM.pixelRef(); |
robertphillips@google.com | ed9866c | 2014-01-09 19:20:45 +0000 | [diff] [blame] | 184 | } |
| 185 | |
skia.committer@gmail.com | 856673a | 2014-01-10 07:08:11 +0000 | [diff] [blame] | 186 | static void drawsprite_proc(SkCanvas* canvas, const SkBitmap& bm, |
robertphillips@google.com | 56bf6e4 | 2014-01-13 13:33:26 +0000 | [diff] [blame] | 187 | const SkBitmap& altBM, const SkPoint& pos, |
| 188 | SkTDArray<SkPixelRef*>* usedPixRefs) { |
robertphillips@google.com | ed9866c | 2014-01-09 19:20:45 +0000 | [diff] [blame] | 189 | const SkMatrix& ctm = canvas->getTotalMatrix(); |
| 190 | |
| 191 | SkPoint p(pos); |
| 192 | ctm.mapPoints(&p, 1); |
| 193 | |
| 194 | canvas->drawSprite(bm, (int)p.fX, (int)p.fY, NULL); |
robertphillips@google.com | 56bf6e4 | 2014-01-13 13:33:26 +0000 | [diff] [blame] | 195 | *usedPixRefs->append() = bm.pixelRef(); |
robertphillips@google.com | ed9866c | 2014-01-09 19:20:45 +0000 | [diff] [blame] | 196 | } |
| 197 | |
| 198 | #if 0 |
| 199 | // Although specifiable, this case doesn't seem to make sense (i.e., the |
| 200 | // bitmap in the shader is never used). |
skia.committer@gmail.com | 856673a | 2014-01-10 07:08:11 +0000 | [diff] [blame] | 201 | static void drawsprite_withshader_proc(SkCanvas* canvas, const SkBitmap& bm, |
robertphillips@google.com | 56bf6e4 | 2014-01-13 13:33:26 +0000 | [diff] [blame] | 202 | const SkBitmap& altBM, const SkPoint& pos, |
| 203 | SkTDArray<SkPixelRef*>* usedPixRefs) { |
robertphillips@google.com | ed9866c | 2014-01-09 19:20:45 +0000 | [diff] [blame] | 204 | SkPaint paint; |
| 205 | init_paint(&paint, bm); |
| 206 | |
| 207 | const SkMatrix& ctm = canvas->getTotalMatrix(); |
| 208 | |
| 209 | SkPoint p(pos); |
| 210 | ctm.mapPoints(&p, 1); |
| 211 | |
| 212 | canvas->drawSprite(altBM, (int)p.fX, (int)p.fY, &paint); |
robertphillips@google.com | 56bf6e4 | 2014-01-13 13:33:26 +0000 | [diff] [blame] | 213 | *usedPixRefs->append() = bm.pixelRef(); |
| 214 | *usedPixRefs->append() = altBM.pixelRef(); |
robertphillips@google.com | ed9866c | 2014-01-09 19:20:45 +0000 | [diff] [blame] | 215 | } |
| 216 | #endif |
| 217 | |
skia.committer@gmail.com | 856673a | 2014-01-10 07:08:11 +0000 | [diff] [blame] | 218 | static void drawbitmaprect_proc(SkCanvas* canvas, const SkBitmap& bm, |
robertphillips@google.com | 56bf6e4 | 2014-01-13 13:33:26 +0000 | [diff] [blame] | 219 | const SkBitmap& altBM, const SkPoint& pos, |
| 220 | SkTDArray<SkPixelRef*>* usedPixRefs) { |
robertphillips@google.com | ed9866c | 2014-01-09 19:20:45 +0000 | [diff] [blame] | 221 | SkRect r = { 0, 0, SkIntToScalar(bm.width()), SkIntToScalar(bm.height()) }; |
| 222 | |
reed@google.com | fe7b1ed | 2012-11-29 21:00:39 +0000 | [diff] [blame] | 223 | r.offset(pos.fX, pos.fY); |
| 224 | canvas->drawBitmapRectToRect(bm, NULL, r, NULL); |
robertphillips@google.com | 56bf6e4 | 2014-01-13 13:33:26 +0000 | [diff] [blame] | 225 | *usedPixRefs->append() = bm.pixelRef(); |
reed@google.com | fe7b1ed | 2012-11-29 21:00:39 +0000 | [diff] [blame] | 226 | } |
| 227 | |
skia.committer@gmail.com | 856673a | 2014-01-10 07:08:11 +0000 | [diff] [blame] | 228 | static void drawbitmaprect_withshader_proc(SkCanvas* canvas, |
| 229 | const SkBitmap& bm, |
| 230 | const SkBitmap& altBM, |
robertphillips@google.com | 56bf6e4 | 2014-01-13 13:33:26 +0000 | [diff] [blame] | 231 | const SkPoint& pos, |
| 232 | SkTDArray<SkPixelRef*>* usedPixRefs) { |
robertphillips@google.com | ed9866c | 2014-01-09 19:20:45 +0000 | [diff] [blame] | 233 | SkPaint paint; |
| 234 | init_paint(&paint, bm); |
| 235 | |
| 236 | SkRect r = { 0, 0, SkIntToScalar(bm.width()), SkIntToScalar(bm.height()) }; |
reed@google.com | fe7b1ed | 2012-11-29 21:00:39 +0000 | [diff] [blame] | 237 | r.offset(pos.fX, pos.fY); |
| 238 | |
robertphillips@google.com | ed9866c | 2014-01-09 19:20:45 +0000 | [diff] [blame] | 239 | // The bitmap in the paint is ignored unless we're drawing an A8 bitmap |
| 240 | canvas->drawBitmapRectToRect(altBM, NULL, r, &paint); |
robertphillips@google.com | 56bf6e4 | 2014-01-13 13:33:26 +0000 | [diff] [blame] | 241 | *usedPixRefs->append() = bm.pixelRef(); |
| 242 | *usedPixRefs->append() = altBM.pixelRef(); |
robertphillips@google.com | ed9866c | 2014-01-09 19:20:45 +0000 | [diff] [blame] | 243 | } |
| 244 | |
skia.committer@gmail.com | 856673a | 2014-01-10 07:08:11 +0000 | [diff] [blame] | 245 | static void drawtext_proc(SkCanvas* canvas, const SkBitmap& bm, |
robertphillips@google.com | 56bf6e4 | 2014-01-13 13:33:26 +0000 | [diff] [blame] | 246 | const SkBitmap& altBM, const SkPoint& pos, |
| 247 | SkTDArray<SkPixelRef*>* usedPixRefs) { |
reed@google.com | fe7b1ed | 2012-11-29 21:00:39 +0000 | [diff] [blame] | 248 | SkPaint paint; |
robertphillips@google.com | ed9866c | 2014-01-09 19:20:45 +0000 | [diff] [blame] | 249 | init_paint(&paint, bm); |
| 250 | paint.setTextSize(SkIntToScalar(1.5*bm.width())); |
| 251 | |
| 252 | canvas->drawText("0", 1, pos.fX, pos.fY+bm.width(), paint); |
robertphillips@google.com | 56bf6e4 | 2014-01-13 13:33:26 +0000 | [diff] [blame] | 253 | *usedPixRefs->append() = bm.pixelRef(); |
robertphillips@google.com | ed9866c | 2014-01-09 19:20:45 +0000 | [diff] [blame] | 254 | } |
| 255 | |
skia.committer@gmail.com | 856673a | 2014-01-10 07:08:11 +0000 | [diff] [blame] | 256 | static void drawpostext_proc(SkCanvas* canvas, const SkBitmap& bm, |
robertphillips@google.com | 56bf6e4 | 2014-01-13 13:33:26 +0000 | [diff] [blame] | 257 | const SkBitmap& altBM, const SkPoint& pos, |
| 258 | SkTDArray<SkPixelRef*>* usedPixRefs) { |
robertphillips@google.com | ed9866c | 2014-01-09 19:20:45 +0000 | [diff] [blame] | 259 | SkPaint paint; |
| 260 | init_paint(&paint, bm); |
| 261 | paint.setTextSize(SkIntToScalar(1.5*bm.width())); |
| 262 | |
| 263 | SkPoint point = { pos.fX, pos.fY + bm.height() }; |
| 264 | canvas->drawPosText("O", 1, &point, paint); |
robertphillips@google.com | 56bf6e4 | 2014-01-13 13:33:26 +0000 | [diff] [blame] | 265 | *usedPixRefs->append() = bm.pixelRef(); |
robertphillips@google.com | ed9866c | 2014-01-09 19:20:45 +0000 | [diff] [blame] | 266 | } |
| 267 | |
skia.committer@gmail.com | 856673a | 2014-01-10 07:08:11 +0000 | [diff] [blame] | 268 | static void drawtextonpath_proc(SkCanvas* canvas, const SkBitmap& bm, |
robertphillips@google.com | 56bf6e4 | 2014-01-13 13:33:26 +0000 | [diff] [blame] | 269 | const SkBitmap& altBM, const SkPoint& pos, |
| 270 | SkTDArray<SkPixelRef*>* usedPixRefs) { |
robertphillips@google.com | ed9866c | 2014-01-09 19:20:45 +0000 | [diff] [blame] | 271 | SkPaint paint; |
| 272 | |
| 273 | init_paint(&paint, bm); |
| 274 | paint.setTextSize(SkIntToScalar(1.5*bm.width())); |
| 275 | |
| 276 | SkPath path; |
| 277 | path.lineTo(SkIntToScalar(bm.width()), 0); |
| 278 | path.offset(pos.fX, pos.fY+bm.height()); |
| 279 | |
| 280 | canvas->drawTextOnPath("O", 1, path, NULL, paint); |
robertphillips@google.com | 56bf6e4 | 2014-01-13 13:33:26 +0000 | [diff] [blame] | 281 | *usedPixRefs->append() = bm.pixelRef(); |
robertphillips@google.com | ed9866c | 2014-01-09 19:20:45 +0000 | [diff] [blame] | 282 | } |
| 283 | |
skia.committer@gmail.com | 856673a | 2014-01-10 07:08:11 +0000 | [diff] [blame] | 284 | static void drawverts_proc(SkCanvas* canvas, const SkBitmap& bm, |
robertphillips@google.com | 56bf6e4 | 2014-01-13 13:33:26 +0000 | [diff] [blame] | 285 | const SkBitmap& altBM, const SkPoint& pos, |
| 286 | SkTDArray<SkPixelRef*>* usedPixRefs) { |
robertphillips@google.com | ed9866c | 2014-01-09 19:20:45 +0000 | [diff] [blame] | 287 | SkPaint paint; |
| 288 | init_paint(&paint, bm); |
| 289 | |
skia.committer@gmail.com | 2e9a715 | 2014-01-14 07:01:42 +0000 | [diff] [blame] | 290 | SkPoint verts[4] = { |
| 291 | { pos.fX, pos.fY }, |
| 292 | { pos.fX + bm.width(), pos.fY }, |
| 293 | { pos.fX + bm.width(), pos.fY + bm.height() }, |
| 294 | { pos.fX, pos.fY + bm.height() } |
robertphillips@google.com | ed9866c | 2014-01-09 19:20:45 +0000 | [diff] [blame] | 295 | }; |
skia.committer@gmail.com | 856673a | 2014-01-10 07:08:11 +0000 | [diff] [blame] | 296 | SkPoint texs[4] = { { 0, 0 }, |
| 297 | { SkIntToScalar(bm.width()), 0 }, |
| 298 | { SkIntToScalar(bm.width()), SkIntToScalar(bm.height()) }, |
robertphillips@google.com | ed9866c | 2014-01-09 19:20:45 +0000 | [diff] [blame] | 299 | { 0, SkIntToScalar(bm.height()) } }; |
| 300 | uint16_t indices[6] = { 0, 1, 2, 0, 2, 3 }; |
| 301 | |
skia.committer@gmail.com | 856673a | 2014-01-10 07:08:11 +0000 | [diff] [blame] | 302 | canvas->drawVertices(SkCanvas::kTriangles_VertexMode, 4, verts, texs, NULL, NULL, |
robertphillips@google.com | ed9866c | 2014-01-09 19:20:45 +0000 | [diff] [blame] | 303 | indices, 6, paint); |
robertphillips@google.com | 56bf6e4 | 2014-01-13 13:33:26 +0000 | [diff] [blame] | 304 | *usedPixRefs->append() = bm.pixelRef(); |
reed@google.com | fe7b1ed | 2012-11-29 21:00:39 +0000 | [diff] [blame] | 305 | } |
| 306 | |
| 307 | // Return a picture with the bitmaps drawn at the specified positions. |
skia.committer@gmail.com | 2e9a715 | 2014-01-14 07:01:42 +0000 | [diff] [blame] | 308 | static SkPicture* record_bitmaps(const SkBitmap bm[], |
robertphillips@google.com | 56bf6e4 | 2014-01-13 13:33:26 +0000 | [diff] [blame] | 309 | const SkPoint pos[], |
| 310 | SkTDArray<SkPixelRef*> analytic[], |
skia.committer@gmail.com | 2e9a715 | 2014-01-14 07:01:42 +0000 | [diff] [blame] | 311 | int count, |
robertphillips@google.com | 56bf6e4 | 2014-01-13 13:33:26 +0000 | [diff] [blame] | 312 | DrawBitmapProc proc) { |
robertphillips@google.com | 84b18c7 | 2014-04-13 19:09:42 +0000 | [diff] [blame] | 313 | SkPictureRecorder recorder; |
robertphillips | 9f1c241 | 2014-06-09 06:25:34 -0700 | [diff] [blame] | 314 | SkCanvas* canvas = recorder.beginRecording(1000, 1000); |
reed@google.com | fe7b1ed | 2012-11-29 21:00:39 +0000 | [diff] [blame] | 315 | for (int i = 0; i < count; ++i) { |
robertphillips@google.com | 56bf6e4 | 2014-01-13 13:33:26 +0000 | [diff] [blame] | 316 | analytic[i].rewind(); |
robertphillips@google.com | ed9866c | 2014-01-09 19:20:45 +0000 | [diff] [blame] | 317 | canvas->save(); |
| 318 | SkRect clipRect = SkRect::MakeXYWH(pos[i].fX, pos[i].fY, |
| 319 | SkIntToScalar(bm[i].width()), |
| 320 | SkIntToScalar(bm[i].height())); |
| 321 | canvas->clipRect(clipRect, SkRegion::kIntersect_Op); |
robertphillips@google.com | 56bf6e4 | 2014-01-13 13:33:26 +0000 | [diff] [blame] | 322 | proc(canvas, bm[i], bm[count+i], pos[i], &analytic[i]); |
robertphillips@google.com | ed9866c | 2014-01-09 19:20:45 +0000 | [diff] [blame] | 323 | canvas->restore(); |
reed@google.com | fe7b1ed | 2012-11-29 21:00:39 +0000 | [diff] [blame] | 324 | } |
robertphillips@google.com | 84b18c7 | 2014-04-13 19:09:42 +0000 | [diff] [blame] | 325 | return recorder.endRecording(); |
reed@google.com | fe7b1ed | 2012-11-29 21:00:39 +0000 | [diff] [blame] | 326 | } |
| 327 | |
commit-bot@chromium.org | e0e7cfe | 2013-09-09 20:09:12 +0000 | [diff] [blame] | 328 | static void rand_rect(SkRect* rect, SkRandom& rand, SkScalar W, SkScalar H) { |
reed@google.com | fe7b1ed | 2012-11-29 21:00:39 +0000 | [diff] [blame] | 329 | rect->fLeft = rand.nextRangeScalar(-W, 2*W); |
| 330 | rect->fTop = rand.nextRangeScalar(-H, 2*H); |
| 331 | rect->fRight = rect->fLeft + rand.nextRangeScalar(0, W); |
| 332 | rect->fBottom = rect->fTop + rand.nextRangeScalar(0, H); |
| 333 | |
| 334 | // we integralize rect to make our tests more predictable, since Gather is |
| 335 | // a little sloppy. |
| 336 | SkIRect ir; |
| 337 | rect->round(&ir); |
| 338 | rect->set(ir); |
| 339 | } |
| 340 | |
robertphillips@google.com | ed9866c | 2014-01-09 19:20:45 +0000 | [diff] [blame] | 341 | static void draw(SkPicture* pic, int width, int height, SkBitmap* result) { |
| 342 | make_bm(result, width, height, SK_ColorBLACK, false); |
reed@google.com | fe7b1ed | 2012-11-29 21:00:39 +0000 | [diff] [blame] | 343 | |
| 344 | SkCanvas canvas(*result); |
robertphillips | 9b14f26 | 2014-06-04 05:40:44 -0700 | [diff] [blame] | 345 | canvas.drawPicture(pic); |
reed@google.com | fe7b1ed | 2012-11-29 21:00:39 +0000 | [diff] [blame] | 346 | } |
| 347 | |
| 348 | template <typename T> int find_index(const T* array, T elem, int count) { |
| 349 | for (int i = 0; i < count; ++i) { |
| 350 | if (array[i] == elem) { |
| 351 | return i; |
| 352 | } |
| 353 | } |
| 354 | return -1; |
| 355 | } |
| 356 | |
| 357 | // Return true if 'ref' is found in array[] |
| 358 | static bool find(SkPixelRef const * const * array, SkPixelRef const * ref, int count) { |
| 359 | return find_index<const SkPixelRef*>(array, ref, count) >= 0; |
| 360 | } |
| 361 | |
skia.committer@gmail.com | 856673a | 2014-01-10 07:08:11 +0000 | [diff] [blame] | 362 | // Look at each pixel that is inside 'subset', and if its color appears in |
| 363 | // colors[], find the corresponding value in refs[] and append that ref into |
robertphillips@google.com | ed9866c | 2014-01-09 19:20:45 +0000 | [diff] [blame] | 364 | // array, skipping duplicates of the same value. |
| 365 | // Note that gathering pixelRefs from rendered colors suffers from the problem |
| 366 | // that multiple simultaneous textures (e.g., A8 for alpha and 8888 for color) |
| 367 | // isn't easy to reconstruct. |
| 368 | static void gather_from_image(const SkBitmap& bm, SkPixelRef* const refs[], |
| 369 | int count, SkTDArray<SkPixelRef*>* array, |
| 370 | const SkRect& subset) { |
| 371 | SkIRect ir; |
| 372 | subset.roundOut(&ir); |
| 373 | |
| 374 | if (!ir.intersect(0, 0, bm.width()-1, bm.height()-1)) { |
| 375 | return; |
| 376 | } |
| 377 | |
reed@google.com | fe7b1ed | 2012-11-29 21:00:39 +0000 | [diff] [blame] | 378 | // Since we only want to return unique values in array, when we scan we just |
| 379 | // set a bit for each index'd color found. In practice we only have a few |
| 380 | // distinct colors, so we just use an int's bits as our array. Hence the |
| 381 | // assert that count <= number-of-bits-in-our-int. |
| 382 | SkASSERT((unsigned)count <= 32); |
| 383 | uint32_t bitarray = 0; |
| 384 | |
| 385 | SkAutoLockPixels alp(bm); |
| 386 | |
robertphillips@google.com | ed9866c | 2014-01-09 19:20:45 +0000 | [diff] [blame] | 387 | for (int y = ir.fTop; y < ir.fBottom; ++y) { |
| 388 | for (int x = ir.fLeft; x < ir.fRight; ++x) { |
reed@google.com | fe7b1ed | 2012-11-29 21:00:39 +0000 | [diff] [blame] | 389 | SkPMColor pmc = *bm.getAddr32(x, y); |
| 390 | // the only good case where the color is not found would be if |
| 391 | // the color is transparent, meaning no bitmap was drawn in that |
| 392 | // pixel. |
| 393 | if (pmc) { |
bsalomon@google.com | c3d753e | 2013-01-08 17:24:44 +0000 | [diff] [blame] | 394 | uint32_t index = SkGetPackedR32(pmc); |
reed@google.com | fe7b1ed | 2012-11-29 21:00:39 +0000 | [diff] [blame] | 395 | SkASSERT(SkGetPackedG32(pmc) == index); |
| 396 | SkASSERT(SkGetPackedB32(pmc) == index); |
robertphillips@google.com | ed9866c | 2014-01-09 19:20:45 +0000 | [diff] [blame] | 397 | if (0 == index) { |
| 398 | continue; // background color |
| 399 | } |
| 400 | SkASSERT(0 == (index - gColorOffset) % gColorScale); |
| 401 | index = (index - gColorOffset) / gColorScale; |
bsalomon@google.com | 5f429b0 | 2013-01-08 18:42:20 +0000 | [diff] [blame] | 402 | SkASSERT(static_cast<int>(index) < count); |
reed@google.com | fe7b1ed | 2012-11-29 21:00:39 +0000 | [diff] [blame] | 403 | bitarray |= 1 << index; |
| 404 | } |
| 405 | } |
| 406 | } |
| 407 | |
| 408 | for (int i = 0; i < count; ++i) { |
| 409 | if (bitarray & (1 << i)) { |
| 410 | *array->append() = refs[i]; |
| 411 | } |
| 412 | } |
| 413 | } |
| 414 | |
robertphillips@google.com | 0e4ce14 | 2014-01-13 13:46:45 +0000 | [diff] [blame] | 415 | static void gather_from_analytic(const SkPoint pos[], SkScalar w, SkScalar h, |
skia.committer@gmail.com | 2e9a715 | 2014-01-14 07:01:42 +0000 | [diff] [blame] | 416 | const SkTDArray<SkPixelRef*> analytic[], |
| 417 | int count, |
| 418 | SkTDArray<SkPixelRef*>* result, |
robertphillips@google.com | 0e4ce14 | 2014-01-13 13:46:45 +0000 | [diff] [blame] | 419 | const SkRect& subset) { |
robertphillips@google.com | 56bf6e4 | 2014-01-13 13:33:26 +0000 | [diff] [blame] | 420 | for (int i = 0; i < count; ++i) { |
| 421 | SkRect rect = SkRect::MakeXYWH(pos[i].fX, pos[i].fY, w, h); |
| 422 | |
| 423 | if (SkRect::Intersects(subset, rect)) { |
| 424 | result->append(analytic[i].count(), analytic[i].begin()); |
| 425 | } |
| 426 | } |
| 427 | } |
| 428 | |
commit-bot@chromium.org | 5bee054 | 2014-03-05 20:59:45 +0000 | [diff] [blame] | 429 | |
| 430 | static const struct { |
| 431 | const DrawBitmapProc proc; |
| 432 | const char* const desc; |
| 433 | } gProcs[] = { |
| 434 | {drawpaint_proc, "drawpaint"}, |
| 435 | {drawpoints_proc, "drawpoints"}, |
| 436 | {drawrect_proc, "drawrect"}, |
| 437 | {drawoval_proc, "drawoval"}, |
| 438 | {drawrrect_proc, "drawrrect"}, |
| 439 | {drawpath_proc, "drawpath"}, |
| 440 | {drawbitmap_proc, "drawbitmap"}, |
| 441 | {drawbitmap_withshader_proc, "drawbitmap_withshader"}, |
| 442 | {drawsprite_proc, "drawsprite"}, |
robertphillips@google.com | 56bf6e4 | 2014-01-13 13:33:26 +0000 | [diff] [blame] | 443 | #if 0 |
commit-bot@chromium.org | 5bee054 | 2014-03-05 20:59:45 +0000 | [diff] [blame] | 444 | {drawsprite_withshader_proc, "drawsprite_withshader"}, |
robertphillips@google.com | 56bf6e4 | 2014-01-13 13:33:26 +0000 | [diff] [blame] | 445 | #endif |
commit-bot@chromium.org | 5bee054 | 2014-03-05 20:59:45 +0000 | [diff] [blame] | 446 | {drawbitmaprect_proc, "drawbitmaprect"}, |
| 447 | {drawbitmaprect_withshader_proc, "drawbitmaprect_withshader"}, |
| 448 | {drawtext_proc, "drawtext"}, |
| 449 | {drawpostext_proc, "drawpostext"}, |
| 450 | {drawtextonpath_proc, "drawtextonpath"}, |
| 451 | {drawverts_proc, "drawverts"}, |
robertphillips@google.com | 56bf6e4 | 2014-01-13 13:33:26 +0000 | [diff] [blame] | 452 | }; |
| 453 | |
| 454 | static void create_textures(SkBitmap* bm, SkPixelRef** refs, int num, int w, int h) { |
skia.committer@gmail.com | 2e9a715 | 2014-01-14 07:01:42 +0000 | [diff] [blame] | 455 | // Our convention is that the color components contain an encoding of |
| 456 | // the index of their corresponding bitmap/pixelref. (0,0,0,0) is |
robertphillips@google.com | 56bf6e4 | 2014-01-13 13:33:26 +0000 | [diff] [blame] | 457 | // reserved for the background |
| 458 | for (int i = 0; i < num; ++i) { |
skia.committer@gmail.com | 2e9a715 | 2014-01-14 07:01:42 +0000 | [diff] [blame] | 459 | make_bm(&bm[i], w, h, |
| 460 | SkColorSetARGB(0xFF, |
| 461 | gColorScale*i+gColorOffset, |
| 462 | gColorScale*i+gColorOffset, |
| 463 | gColorScale*i+gColorOffset), |
robertphillips@google.com | 56bf6e4 | 2014-01-13 13:33:26 +0000 | [diff] [blame] | 464 | true); |
| 465 | refs[i] = bm[i].pixelRef(); |
| 466 | } |
| 467 | |
| 468 | // The A8 alternate bitmaps are all BW checkerboards |
| 469 | for (int i = 0; i < num; ++i) { |
| 470 | make_checkerboard(&bm[num+i], w, h, true); |
| 471 | refs[num+i] = bm[num+i].pixelRef(); |
| 472 | } |
| 473 | } |
| 474 | |
reed@google.com | fe7b1ed | 2012-11-29 21:00:39 +0000 | [diff] [blame] | 475 | static void test_gatherpixelrefs(skiatest::Reporter* reporter) { |
robertphillips@google.com | ed9866c | 2014-01-09 19:20:45 +0000 | [diff] [blame] | 476 | const int IW = 32; |
reed@google.com | fe7b1ed | 2012-11-29 21:00:39 +0000 | [diff] [blame] | 477 | const int IH = IW; |
| 478 | const SkScalar W = SkIntToScalar(IW); |
| 479 | const SkScalar H = W; |
| 480 | |
| 481 | static const int N = 4; |
robertphillips@google.com | ed9866c | 2014-01-09 19:20:45 +0000 | [diff] [blame] | 482 | SkBitmap bm[2*N]; |
| 483 | SkPixelRef* refs[2*N]; |
robertphillips@google.com | 56bf6e4 | 2014-01-13 13:33:26 +0000 | [diff] [blame] | 484 | SkTDArray<SkPixelRef*> analytic[N]; |
reed@google.com | fe7b1ed | 2012-11-29 21:00:39 +0000 | [diff] [blame] | 485 | |
robertphillips@google.com | ed9866c | 2014-01-09 19:20:45 +0000 | [diff] [blame] | 486 | const SkPoint pos[N] = { |
reed@google.com | fe7b1ed | 2012-11-29 21:00:39 +0000 | [diff] [blame] | 487 | { 0, 0 }, { W, 0 }, { 0, H }, { W, H } |
| 488 | }; |
| 489 | |
robertphillips@google.com | 56bf6e4 | 2014-01-13 13:33:26 +0000 | [diff] [blame] | 490 | create_textures(bm, refs, N, IW, IH); |
reed@google.com | fe7b1ed | 2012-11-29 21:00:39 +0000 | [diff] [blame] | 491 | |
commit-bot@chromium.org | e0e7cfe | 2013-09-09 20:09:12 +0000 | [diff] [blame] | 492 | SkRandom rand; |
robertphillips@google.com | 56bf6e4 | 2014-01-13 13:33:26 +0000 | [diff] [blame] | 493 | for (size_t k = 0; k < SK_ARRAY_COUNT(gProcs); ++k) { |
commit-bot@chromium.org | 5bee054 | 2014-03-05 20:59:45 +0000 | [diff] [blame] | 494 | SkAutoTUnref<SkPicture> pic( |
| 495 | record_bitmaps(bm, pos, analytic, N, gProcs[k].proc)); |
reed@google.com | fe7b1ed | 2012-11-29 21:00:39 +0000 | [diff] [blame] | 496 | |
tomhudson@google.com | 381010e | 2013-10-24 11:12:47 +0000 | [diff] [blame] | 497 | REPORTER_ASSERT(reporter, pic->willPlayBackBitmaps() || N == 0); |
reed@google.com | fe7b1ed | 2012-11-29 21:00:39 +0000 | [diff] [blame] | 498 | // quick check for a small piece of each quadrant, which should just |
robertphillips@google.com | 56bf6e4 | 2014-01-13 13:33:26 +0000 | [diff] [blame] | 499 | // contain 1 or 2 bitmaps. |
reed@google.com | fe7b1ed | 2012-11-29 21:00:39 +0000 | [diff] [blame] | 500 | for (size_t i = 0; i < SK_ARRAY_COUNT(pos); ++i) { |
| 501 | SkRect r; |
| 502 | r.set(2, 2, W - 2, H - 2); |
| 503 | r.offset(pos[i].fX, pos[i].fY); |
| 504 | SkAutoDataUnref data(SkPictureUtils::GatherPixelRefs(pic, r)); |
commit-bot@chromium.org | 5bee054 | 2014-03-05 20:59:45 +0000 | [diff] [blame] | 505 | if (!data) { |
| 506 | ERRORF(reporter, "SkPictureUtils::GatherPixelRefs returned " |
| 507 | "NULL for %s.", gProcs[k].desc); |
| 508 | continue; |
| 509 | } |
| 510 | SkPixelRef** gatheredRefs = (SkPixelRef**)data->data(); |
| 511 | int count = static_cast<int>(data->size() / sizeof(SkPixelRef*)); |
| 512 | REPORTER_ASSERT(reporter, 1 == count || 2 == count); |
| 513 | if (1 == count) { |
| 514 | REPORTER_ASSERT(reporter, gatheredRefs[0] == refs[i]); |
| 515 | } else if (2 == count) { |
| 516 | REPORTER_ASSERT(reporter, |
| 517 | (gatheredRefs[0] == refs[i] && gatheredRefs[1] == refs[i+N]) || |
| 518 | (gatheredRefs[1] == refs[i] && gatheredRefs[0] == refs[i+N])); |
commit-bot@chromium.org | fe433c1 | 2013-07-09 16:04:32 +0000 | [diff] [blame] | 519 | } |
reed@google.com | fe7b1ed | 2012-11-29 21:00:39 +0000 | [diff] [blame] | 520 | } |
| 521 | |
robertphillips@google.com | ed9866c | 2014-01-09 19:20:45 +0000 | [diff] [blame] | 522 | SkBitmap image; |
| 523 | draw(pic, 2*IW, 2*IH, &image); |
| 524 | |
reed@google.com | fe7b1ed | 2012-11-29 21:00:39 +0000 | [diff] [blame] | 525 | // Test a bunch of random (mostly) rects, and compare the gather results |
| 526 | // with a deduced list of refs by looking at the colors drawn. |
| 527 | for (int j = 0; j < 100; ++j) { |
| 528 | SkRect r; |
| 529 | rand_rect(&r, rand, 2*W, 2*H); |
| 530 | |
robertphillips@google.com | 56bf6e4 | 2014-01-13 13:33:26 +0000 | [diff] [blame] | 531 | SkTDArray<SkPixelRef*> fromImage; |
| 532 | gather_from_image(image, refs, N, &fromImage, r); |
| 533 | |
| 534 | SkTDArray<SkPixelRef*> fromAnalytic; |
| 535 | gather_from_analytic(pos, W, H, analytic, N, &fromAnalytic, r); |
reed@google.com | fe7b1ed | 2012-11-29 21:00:39 +0000 | [diff] [blame] | 536 | |
| 537 | SkData* data = SkPictureUtils::GatherPixelRefs(pic, r); |
| 538 | size_t dataSize = data ? data->size() : 0; |
robertphillips@google.com | e9cd27d | 2013-10-16 17:48:11 +0000 | [diff] [blame] | 539 | int gatherCount = static_cast<int>(dataSize / sizeof(SkPixelRef*)); |
reed@google.com | fe7b1ed | 2012-11-29 21:00:39 +0000 | [diff] [blame] | 540 | SkASSERT(gatherCount * sizeof(SkPixelRef*) == dataSize); |
| 541 | SkPixelRef** gatherRefs = data ? (SkPixelRef**)(data->data()) : NULL; |
| 542 | SkAutoDataUnref adu(data); |
| 543 | |
robertphillips@google.com | 56bf6e4 | 2014-01-13 13:33:26 +0000 | [diff] [blame] | 544 | // Everything that we saw drawn should appear in the analytic list |
| 545 | // but the analytic list may contain some pixelRefs that were not |
| 546 | // seen in the image (e.g., A8 textures used as masks) |
| 547 | for (int i = 0; i < fromImage.count(); ++i) { |
commit-bot@chromium.org | 5bee054 | 2014-03-05 20:59:45 +0000 | [diff] [blame] | 548 | if (-1 == fromAnalytic.find(fromImage[i])) { |
| 549 | ERRORF(reporter, "PixelRef missing %d %s", |
| 550 | i, gProcs[k].desc); |
| 551 | } |
robertphillips@google.com | 56bf6e4 | 2014-01-13 13:33:26 +0000 | [diff] [blame] | 552 | } |
skia.committer@gmail.com | c3d7d90 | 2012-11-30 02:01:24 +0000 | [diff] [blame] | 553 | |
reed@google.com | fe7b1ed | 2012-11-29 21:00:39 +0000 | [diff] [blame] | 554 | /* |
| 555 | * GatherPixelRefs is conservative, so it can return more bitmaps |
robertphillips@google.com | 56bf6e4 | 2014-01-13 13:33:26 +0000 | [diff] [blame] | 556 | * than are strictly required. Thus our check here is only that |
| 557 | * Gather didn't miss any that we actually needed. Even that isn't |
reed@google.com | fe7b1ed | 2012-11-29 21:00:39 +0000 | [diff] [blame] | 558 | * a strict requirement on Gather, which is meant to be quick and |
| 559 | * only mostly-correct, but at the moment this test should work. |
| 560 | */ |
robertphillips@google.com | 56bf6e4 | 2014-01-13 13:33:26 +0000 | [diff] [blame] | 561 | for (int i = 0; i < fromAnalytic.count(); ++i) { |
| 562 | bool found = find(gatherRefs, fromAnalytic[i], gatherCount); |
commit-bot@chromium.org | 5bee054 | 2014-03-05 20:59:45 +0000 | [diff] [blame] | 563 | if (!found) { |
| 564 | ERRORF(reporter, "PixelRef missing %d %s", |
| 565 | i, gProcs[k].desc); |
| 566 | } |
reed@google.com | fe7b1ed | 2012-11-29 21:00:39 +0000 | [diff] [blame] | 567 | #if 0 |
| 568 | // enable this block of code to debug failures, as it will rerun |
| 569 | // the case that failed. |
| 570 | if (!found) { |
| 571 | SkData* data = SkPictureUtils::GatherPixelRefs(pic, r); |
| 572 | size_t dataSize = data ? data->size() : 0; |
| 573 | } |
| 574 | #endif |
| 575 | } |
| 576 | } |
| 577 | } |
| 578 | } |
| 579 | |
robertphillips@google.com | 56bf6e4 | 2014-01-13 13:33:26 +0000 | [diff] [blame] | 580 | static void test_gatherpixelrefsandrects(skiatest::Reporter* reporter) { |
| 581 | const int IW = 32; |
| 582 | const int IH = IW; |
| 583 | const SkScalar W = SkIntToScalar(IW); |
| 584 | const SkScalar H = W; |
| 585 | |
| 586 | static const int N = 4; |
| 587 | SkBitmap bm[2*N]; |
| 588 | SkPixelRef* refs[2*N]; |
| 589 | SkTDArray<SkPixelRef*> analytic[N]; |
| 590 | |
| 591 | const SkPoint pos[N] = { |
| 592 | { 0, 0 }, { W, 0 }, { 0, H }, { W, H } |
| 593 | }; |
| 594 | |
| 595 | create_textures(bm, refs, N, IW, IH); |
| 596 | |
| 597 | SkRandom rand; |
| 598 | for (size_t k = 0; k < SK_ARRAY_COUNT(gProcs); ++k) { |
commit-bot@chromium.org | 5bee054 | 2014-03-05 20:59:45 +0000 | [diff] [blame] | 599 | SkAutoTUnref<SkPicture> pic( |
| 600 | record_bitmaps(bm, pos, analytic, N, gProcs[k].proc)); |
robertphillips@google.com | 56bf6e4 | 2014-01-13 13:33:26 +0000 | [diff] [blame] | 601 | |
| 602 | REPORTER_ASSERT(reporter, pic->willPlayBackBitmaps() || N == 0); |
| 603 | |
| 604 | SkAutoTUnref<SkPictureUtils::SkPixelRefContainer> prCont( |
| 605 | new SkPictureUtils::SkPixelRefsAndRectsList); |
| 606 | |
| 607 | SkPictureUtils::GatherPixelRefsAndRects(pic, prCont); |
| 608 | |
| 609 | // quick check for a small piece of each quadrant, which should just |
| 610 | // contain 1 or 2 bitmaps. |
| 611 | for (size_t i = 0; i < SK_ARRAY_COUNT(pos); ++i) { |
| 612 | SkRect r; |
| 613 | r.set(2, 2, W - 2, H - 2); |
| 614 | r.offset(pos[i].fX, pos[i].fY); |
| 615 | |
| 616 | SkTDArray<SkPixelRef*> gatheredRefs; |
| 617 | prCont->query(r, &gatheredRefs); |
| 618 | |
| 619 | int count = gatheredRefs.count(); |
| 620 | REPORTER_ASSERT(reporter, 1 == count || 2 == count); |
| 621 | if (1 == count) { |
| 622 | REPORTER_ASSERT(reporter, gatheredRefs[0] == refs[i]); |
| 623 | } else if (2 == count) { |
skia.committer@gmail.com | 2e9a715 | 2014-01-14 07:01:42 +0000 | [diff] [blame] | 624 | REPORTER_ASSERT(reporter, |
robertphillips@google.com | 56bf6e4 | 2014-01-13 13:33:26 +0000 | [diff] [blame] | 625 | (gatheredRefs[0] == refs[i] && gatheredRefs[1] == refs[i+N]) || |
| 626 | (gatheredRefs[1] == refs[i] && gatheredRefs[0] == refs[i+N])); |
| 627 | } |
| 628 | } |
| 629 | |
| 630 | SkBitmap image; |
| 631 | draw(pic, 2*IW, 2*IH, &image); |
| 632 | |
| 633 | // Test a bunch of random (mostly) rects, and compare the gather results |
| 634 | // with the analytic results and the pixel refs seen in a rendering. |
| 635 | for (int j = 0; j < 100; ++j) { |
| 636 | SkRect r; |
| 637 | rand_rect(&r, rand, 2*W, 2*H); |
| 638 | |
| 639 | SkTDArray<SkPixelRef*> fromImage; |
| 640 | gather_from_image(image, refs, N, &fromImage, r); |
| 641 | |
| 642 | SkTDArray<SkPixelRef*> fromAnalytic; |
| 643 | gather_from_analytic(pos, W, H, analytic, N, &fromAnalytic, r); |
| 644 | |
| 645 | SkTDArray<SkPixelRef*> gatheredRefs; |
| 646 | prCont->query(r, &gatheredRefs); |
| 647 | |
| 648 | // Everything that we saw drawn should appear in the analytic list |
| 649 | // but the analytic list may contain some pixelRefs that were not |
| 650 | // seen in the image (e.g., A8 textures used as masks) |
| 651 | for (int i = 0; i < fromImage.count(); ++i) { |
| 652 | REPORTER_ASSERT(reporter, -1 != fromAnalytic.find(fromImage[i])); |
| 653 | } |
| 654 | |
| 655 | // Everything in the analytic list should appear in the gathered |
skia.committer@gmail.com | 2e9a715 | 2014-01-14 07:01:42 +0000 | [diff] [blame] | 656 | // list. |
robertphillips@google.com | 56bf6e4 | 2014-01-13 13:33:26 +0000 | [diff] [blame] | 657 | for (int i = 0; i < fromAnalytic.count(); ++i) { |
| 658 | REPORTER_ASSERT(reporter, -1 != gatheredRefs.find(fromAnalytic[i])); |
| 659 | } |
| 660 | } |
| 661 | } |
| 662 | } |
| 663 | |
scroggo@google.com | d614c6a | 2012-09-14 17:26:37 +0000 | [diff] [blame] | 664 | #ifdef SK_DEBUG |
robertphillips | db53990 | 2014-07-01 08:47:04 -0700 | [diff] [blame] | 665 | // Ensure that deleting an empty SkPicture does not assert. Asserts only fire |
| 666 | // in debug mode, so only run in debug mode. |
| 667 | static void test_deleting_empty_picture() { |
robertphillips@google.com | 84b18c7 | 2014-04-13 19:09:42 +0000 | [diff] [blame] | 668 | SkPictureRecorder recorder; |
scroggo@google.com | d614c6a | 2012-09-14 17:26:37 +0000 | [diff] [blame] | 669 | // Creates an SkPictureRecord |
robertphillips | 9f1c241 | 2014-06-09 06:25:34 -0700 | [diff] [blame] | 670 | recorder.beginRecording(0, 0); |
robertphillips | db53990 | 2014-07-01 08:47:04 -0700 | [diff] [blame] | 671 | // Turns that into an SkPicture |
robertphillips@google.com | 84b18c7 | 2014-04-13 19:09:42 +0000 | [diff] [blame] | 672 | SkAutoTUnref<SkPicture> picture(recorder.endRecording()); |
robertphillips | db53990 | 2014-07-01 08:47:04 -0700 | [diff] [blame] | 673 | // Ceates a new SkPictureRecord |
robertphillips | 9f1c241 | 2014-06-09 06:25:34 -0700 | [diff] [blame] | 674 | recorder.beginRecording(0, 0); |
scroggo@google.com | d614c6a | 2012-09-14 17:26:37 +0000 | [diff] [blame] | 675 | } |
| 676 | |
| 677 | // Ensure that serializing an empty picture does not assert. Likewise only runs in debug mode. |
| 678 | static void test_serializing_empty_picture() { |
robertphillips@google.com | 84b18c7 | 2014-04-13 19:09:42 +0000 | [diff] [blame] | 679 | SkPictureRecorder recorder; |
robertphillips | 9f1c241 | 2014-06-09 06:25:34 -0700 | [diff] [blame] | 680 | recorder.beginRecording(0, 0); |
robertphillips@google.com | 84b18c7 | 2014-04-13 19:09:42 +0000 | [diff] [blame] | 681 | SkAutoTUnref<SkPicture> picture(recorder.endRecording()); |
scroggo@google.com | d614c6a | 2012-09-14 17:26:37 +0000 | [diff] [blame] | 682 | SkDynamicMemoryWStream stream; |
robertphillips@google.com | 84b18c7 | 2014-04-13 19:09:42 +0000 | [diff] [blame] | 683 | picture->serialize(&stream); |
scroggo@google.com | d614c6a | 2012-09-14 17:26:37 +0000 | [diff] [blame] | 684 | } |
| 685 | #endif |
| 686 | |
commit-bot@chromium.org | e0e7cfe | 2013-09-09 20:09:12 +0000 | [diff] [blame] | 687 | static void rand_op(SkCanvas* canvas, SkRandom& rand) { |
reed@google.com | 21b519d | 2012-10-02 17:42:15 +0000 | [diff] [blame] | 688 | SkPaint paint; |
| 689 | SkRect rect = SkRect::MakeWH(50, 50); |
| 690 | |
| 691 | SkScalar unit = rand.nextUScalar1(); |
| 692 | if (unit <= 0.3) { |
| 693 | // SkDebugf("save\n"); |
| 694 | canvas->save(); |
| 695 | } else if (unit <= 0.6) { |
| 696 | // SkDebugf("restore\n"); |
| 697 | canvas->restore(); |
| 698 | } else if (unit <= 0.9) { |
| 699 | // SkDebugf("clip\n"); |
| 700 | canvas->clipRect(rect); |
| 701 | } else { |
| 702 | // SkDebugf("draw\n"); |
| 703 | canvas->drawPaint(paint); |
| 704 | } |
| 705 | } |
| 706 | |
robertphillips@google.com | b950c6f | 2014-04-25 00:02:12 +0000 | [diff] [blame] | 707 | #if SK_SUPPORT_GPU |
commit-bot@chromium.org | e2cb12a | 2014-04-24 21:53:13 +0000 | [diff] [blame] | 708 | static void test_gpu_veto(skiatest::Reporter* reporter) { |
| 709 | |
| 710 | SkPictureRecorder recorder; |
| 711 | |
robertphillips | 9f1c241 | 2014-06-09 06:25:34 -0700 | [diff] [blame] | 712 | SkCanvas* canvas = recorder.beginRecording(100, 100); |
commit-bot@chromium.org | e2cb12a | 2014-04-24 21:53:13 +0000 | [diff] [blame] | 713 | { |
| 714 | SkPath path; |
| 715 | path.moveTo(0, 0); |
| 716 | path.lineTo(50, 50); |
| 717 | |
| 718 | SkScalar intervals[] = { 1.0f, 1.0f }; |
| 719 | SkAutoTUnref<SkDashPathEffect> dash(SkDashPathEffect::Create(intervals, 2, 0)); |
| 720 | |
| 721 | SkPaint paint; |
| 722 | paint.setStyle(SkPaint::kStroke_Style); |
| 723 | paint.setPathEffect(dash); |
| 724 | |
| 725 | canvas->drawPath(path, paint); |
| 726 | } |
| 727 | SkAutoTUnref<SkPicture> picture(recorder.endRecording()); |
| 728 | // path effects currently render an SkPicture undesireable for GPU rendering |
commit-bot@chromium.org | a1ff26a | 2014-05-30 21:52:52 +0000 | [diff] [blame] | 729 | |
| 730 | const char *reason = NULL; |
| 731 | REPORTER_ASSERT(reporter, !picture->suitableForGpuRasterization(NULL, &reason)); |
| 732 | REPORTER_ASSERT(reporter, NULL != reason); |
commit-bot@chromium.org | e2cb12a | 2014-04-24 21:53:13 +0000 | [diff] [blame] | 733 | |
robertphillips | 9f1c241 | 2014-06-09 06:25:34 -0700 | [diff] [blame] | 734 | canvas = recorder.beginRecording(100, 100); |
commit-bot@chromium.org | e2cb12a | 2014-04-24 21:53:13 +0000 | [diff] [blame] | 735 | { |
| 736 | SkPath path; |
| 737 | |
| 738 | path.moveTo(0, 0); |
| 739 | path.lineTo(0, 50); |
| 740 | path.lineTo(25, 25); |
| 741 | path.lineTo(50, 50); |
| 742 | path.lineTo(50, 0); |
| 743 | path.close(); |
| 744 | REPORTER_ASSERT(reporter, !path.isConvex()); |
| 745 | |
| 746 | SkPaint paint; |
| 747 | paint.setAntiAlias(true); |
| 748 | for (int i = 0; i < 50; ++i) { |
| 749 | canvas->drawPath(path, paint); |
| 750 | } |
| 751 | } |
| 752 | picture.reset(recorder.endRecording()); |
| 753 | // A lot of AA concave paths currently render an SkPicture undesireable for GPU rendering |
| 754 | REPORTER_ASSERT(reporter, !picture->suitableForGpuRasterization(NULL)); |
| 755 | |
robertphillips | 9f1c241 | 2014-06-09 06:25:34 -0700 | [diff] [blame] | 756 | canvas = recorder.beginRecording(100, 100); |
commit-bot@chromium.org | e2cb12a | 2014-04-24 21:53:13 +0000 | [diff] [blame] | 757 | { |
| 758 | SkPath path; |
| 759 | |
| 760 | path.moveTo(0, 0); |
| 761 | path.lineTo(0, 50); |
| 762 | path.lineTo(25, 25); |
| 763 | path.lineTo(50, 50); |
| 764 | path.lineTo(50, 0); |
| 765 | path.close(); |
| 766 | REPORTER_ASSERT(reporter, !path.isConvex()); |
| 767 | |
| 768 | SkPaint paint; |
| 769 | paint.setAntiAlias(true); |
| 770 | paint.setStyle(SkPaint::kStroke_Style); |
| 771 | paint.setStrokeWidth(0); |
| 772 | for (int i = 0; i < 50; ++i) { |
| 773 | canvas->drawPath(path, paint); |
| 774 | } |
| 775 | } |
| 776 | picture.reset(recorder.endRecording()); |
| 777 | // hairline stroked AA concave paths are fine for GPU rendering |
| 778 | REPORTER_ASSERT(reporter, picture->suitableForGpuRasterization(NULL)); |
| 779 | } |
commit-bot@chromium.org | 0205aba | 2014-05-06 12:02:22 +0000 | [diff] [blame] | 780 | |
| 781 | static void test_gpu_picture_optimization(skiatest::Reporter* reporter, |
| 782 | GrContextFactory* factory) { |
bsalomon | e904c09 | 2014-07-17 10:50:59 -0700 | [diff] [blame] | 783 | for (int i= 0; i < GrContextFactory::kGLContextTypeCnt; ++i) { |
| 784 | GrContextFactory::GLContextType glCtxType = (GrContextFactory::GLContextType) i; |
commit-bot@chromium.org | 0205aba | 2014-05-06 12:02:22 +0000 | [diff] [blame] | 785 | |
bsalomon | e904c09 | 2014-07-17 10:50:59 -0700 | [diff] [blame] | 786 | if (!GrContextFactory::IsRenderingGLContext(glCtxType)) { |
| 787 | continue; |
| 788 | } |
commit-bot@chromium.org | 0205aba | 2014-05-06 12:02:22 +0000 | [diff] [blame] | 789 | |
bsalomon | e904c09 | 2014-07-17 10:50:59 -0700 | [diff] [blame] | 790 | GrContext* context = factory->get(glCtxType); |
commit-bot@chromium.org | 0205aba | 2014-05-06 12:02:22 +0000 | [diff] [blame] | 791 | |
bsalomon | e904c09 | 2014-07-17 10:50:59 -0700 | [diff] [blame] | 792 | if (NULL == context) { |
| 793 | continue; |
| 794 | } |
commit-bot@chromium.org | 0205aba | 2014-05-06 12:02:22 +0000 | [diff] [blame] | 795 | |
bsalomon | e904c09 | 2014-07-17 10:50:59 -0700 | [diff] [blame] | 796 | static const int kWidth = 100; |
| 797 | static const int kHeight = 100; |
commit-bot@chromium.org | 0205aba | 2014-05-06 12:02:22 +0000 | [diff] [blame] | 798 | |
bsalomon | e904c09 | 2014-07-17 10:50:59 -0700 | [diff] [blame] | 799 | SkAutoTUnref<SkPicture> pict; |
| 800 | |
| 801 | // create a picture with the structure: |
commit-bot@chromium.org | 0205aba | 2014-05-06 12:02:22 +0000 | [diff] [blame] | 802 | // 1) |
bsalomon | e904c09 | 2014-07-17 10:50:59 -0700 | [diff] [blame] | 803 | // SaveLayer |
| 804 | // Restore |
commit-bot@chromium.org | 0205aba | 2014-05-06 12:02:22 +0000 | [diff] [blame] | 805 | // 2) |
bsalomon | e904c09 | 2014-07-17 10:50:59 -0700 | [diff] [blame] | 806 | // SaveLayer |
| 807 | // Translate |
| 808 | // SaveLayer w/ bound |
| 809 | // Restore |
| 810 | // Restore |
commit-bot@chromium.org | 0205aba | 2014-05-06 12:02:22 +0000 | [diff] [blame] | 811 | // 3) |
bsalomon | e904c09 | 2014-07-17 10:50:59 -0700 | [diff] [blame] | 812 | // SaveLayer w/ copyable paint |
| 813 | // Restore |
commit-bot@chromium.org | 0205aba | 2014-05-06 12:02:22 +0000 | [diff] [blame] | 814 | // 4) |
bsalomon | e904c09 | 2014-07-17 10:50:59 -0700 | [diff] [blame] | 815 | // SaveLayer w/ non-copyable paint |
| 816 | // Restore |
commit-bot@chromium.org | 0205aba | 2014-05-06 12:02:22 +0000 | [diff] [blame] | 817 | { |
bsalomon | e904c09 | 2014-07-17 10:50:59 -0700 | [diff] [blame] | 818 | SkPictureRecorder recorder; |
| 819 | |
| 820 | SkCanvas* c = recorder.beginRecording(kWidth, kHeight); |
| 821 | // 1) |
| 822 | c->saveLayer(NULL, NULL); |
commit-bot@chromium.org | 0205aba | 2014-05-06 12:02:22 +0000 | [diff] [blame] | 823 | c->restore(); |
bsalomon | e904c09 | 2014-07-17 10:50:59 -0700 | [diff] [blame] | 824 | |
| 825 | // 2) |
| 826 | c->saveLayer(NULL, NULL); |
| 827 | c->translate(kWidth/2, kHeight/2); |
| 828 | SkRect r = SkRect::MakeXYWH(0, 0, kWidth/2, kHeight/2); |
| 829 | c->saveLayer(&r, NULL); |
| 830 | c->restore(); |
| 831 | c->restore(); |
| 832 | |
| 833 | // 3) |
| 834 | { |
| 835 | SkPaint p; |
| 836 | p.setColor(SK_ColorRED); |
| 837 | c->saveLayer(NULL, &p); |
| 838 | c->restore(); |
| 839 | } |
| 840 | // 4) |
| 841 | // TODO: this case will need to be removed once the paint's are immutable |
| 842 | { |
| 843 | SkPaint p; |
| 844 | SkAutoTUnref<SkColorFilter> cf(SkLumaColorFilter::Create()); |
| 845 | p.setImageFilter(SkColorFilterImageFilter::Create(cf.get()))->unref(); |
| 846 | c->saveLayer(NULL, &p); |
| 847 | c->restore(); |
| 848 | } |
| 849 | |
| 850 | pict.reset(recorder.endRecording()); |
commit-bot@chromium.org | 0205aba | 2014-05-06 12:02:22 +0000 | [diff] [blame] | 851 | } |
| 852 | |
bsalomon | e904c09 | 2014-07-17 10:50:59 -0700 | [diff] [blame] | 853 | // Now test out the SaveLayer extraction |
| 854 | { |
| 855 | SkImageInfo info = SkImageInfo::MakeN32Premul(kWidth, kHeight); |
commit-bot@chromium.org | 0205aba | 2014-05-06 12:02:22 +0000 | [diff] [blame] | 856 | |
bsalomon | e904c09 | 2014-07-17 10:50:59 -0700 | [diff] [blame] | 857 | SkAutoTUnref<SkSurface> surface(SkSurface::NewScratchRenderTarget(context, info)); |
commit-bot@chromium.org | 0205aba | 2014-05-06 12:02:22 +0000 | [diff] [blame] | 858 | |
bsalomon | e904c09 | 2014-07-17 10:50:59 -0700 | [diff] [blame] | 859 | SkCanvas* canvas = surface->getCanvas(); |
commit-bot@chromium.org | 0205aba | 2014-05-06 12:02:22 +0000 | [diff] [blame] | 860 | |
bsalomon | e904c09 | 2014-07-17 10:50:59 -0700 | [diff] [blame] | 861 | canvas->EXPERIMENTAL_optimize(pict); |
commit-bot@chromium.org | 0205aba | 2014-05-06 12:02:22 +0000 | [diff] [blame] | 862 | |
bsalomon | e904c09 | 2014-07-17 10:50:59 -0700 | [diff] [blame] | 863 | SkPicture::AccelData::Key key = GPUAccelData::ComputeAccelDataKey(); |
commit-bot@chromium.org | 0205aba | 2014-05-06 12:02:22 +0000 | [diff] [blame] | 864 | |
bsalomon | e904c09 | 2014-07-17 10:50:59 -0700 | [diff] [blame] | 865 | const SkPicture::AccelData* data = pict->EXPERIMENTAL_getAccelData(key); |
| 866 | REPORTER_ASSERT(reporter, NULL != data); |
commit-bot@chromium.org | 0205aba | 2014-05-06 12:02:22 +0000 | [diff] [blame] | 867 | |
bsalomon | e904c09 | 2014-07-17 10:50:59 -0700 | [diff] [blame] | 868 | const GPUAccelData *gpuData = static_cast<const GPUAccelData*>(data); |
| 869 | REPORTER_ASSERT(reporter, 5 == gpuData->numSaveLayers()); |
commit-bot@chromium.org | 0205aba | 2014-05-06 12:02:22 +0000 | [diff] [blame] | 870 | |
bsalomon | e904c09 | 2014-07-17 10:50:59 -0700 | [diff] [blame] | 871 | const GPUAccelData::SaveLayerInfo& info0 = gpuData->saveLayerInfo(0); |
| 872 | // The parent/child layer appear in reverse order |
| 873 | const GPUAccelData::SaveLayerInfo& info1 = gpuData->saveLayerInfo(2); |
| 874 | const GPUAccelData::SaveLayerInfo& info2 = gpuData->saveLayerInfo(1); |
| 875 | const GPUAccelData::SaveLayerInfo& info3 = gpuData->saveLayerInfo(3); |
reed@google.com | 47b679b | 2014-05-14 18:58:16 +0000 | [diff] [blame] | 876 | // const GPUAccelData::SaveLayerInfo& info4 = gpuData->saveLayerInfo(4); |
commit-bot@chromium.org | 0205aba | 2014-05-06 12:02:22 +0000 | [diff] [blame] | 877 | |
bsalomon | e904c09 | 2014-07-17 10:50:59 -0700 | [diff] [blame] | 878 | REPORTER_ASSERT(reporter, info0.fValid); |
| 879 | REPORTER_ASSERT(reporter, kWidth == info0.fSize.fWidth && kHeight == info0.fSize.fHeight); |
| 880 | REPORTER_ASSERT(reporter, info0.fCTM.isIdentity()); |
| 881 | REPORTER_ASSERT(reporter, 0 == info0.fOffset.fX && 0 == info0.fOffset.fY); |
| 882 | REPORTER_ASSERT(reporter, NULL != info0.fPaint); |
| 883 | REPORTER_ASSERT(reporter, !info0.fIsNested && !info0.fHasNestedLayers); |
commit-bot@chromium.org | 0205aba | 2014-05-06 12:02:22 +0000 | [diff] [blame] | 884 | |
bsalomon | e904c09 | 2014-07-17 10:50:59 -0700 | [diff] [blame] | 885 | REPORTER_ASSERT(reporter, info1.fValid); |
| 886 | REPORTER_ASSERT(reporter, kWidth == info1.fSize.fWidth && kHeight == info1.fSize.fHeight); |
| 887 | REPORTER_ASSERT(reporter, info1.fCTM.isIdentity()); |
| 888 | REPORTER_ASSERT(reporter, 0 == info1.fOffset.fX && 0 == info1.fOffset.fY); |
| 889 | REPORTER_ASSERT(reporter, NULL != info1.fPaint); |
| 890 | REPORTER_ASSERT(reporter, !info1.fIsNested && info1.fHasNestedLayers); // has a nested SL |
commit-bot@chromium.org | 0205aba | 2014-05-06 12:02:22 +0000 | [diff] [blame] | 891 | |
bsalomon | e904c09 | 2014-07-17 10:50:59 -0700 | [diff] [blame] | 892 | REPORTER_ASSERT(reporter, info2.fValid); |
| 893 | REPORTER_ASSERT(reporter, kWidth/2 == info2.fSize.fWidth && |
| 894 | kHeight/2 == info2.fSize.fHeight); // bound reduces size |
| 895 | REPORTER_ASSERT(reporter, info2.fCTM.isIdentity()); // translated |
| 896 | REPORTER_ASSERT(reporter, kWidth/2 == info2.fOffset.fX && |
| 897 | kHeight/2 == info2.fOffset.fY); |
| 898 | REPORTER_ASSERT(reporter, NULL != info1.fPaint); |
| 899 | REPORTER_ASSERT(reporter, info2.fIsNested && !info2.fHasNestedLayers); // is nested |
commit-bot@chromium.org | 0205aba | 2014-05-06 12:02:22 +0000 | [diff] [blame] | 900 | |
bsalomon | e904c09 | 2014-07-17 10:50:59 -0700 | [diff] [blame] | 901 | REPORTER_ASSERT(reporter, info3.fValid); |
| 902 | REPORTER_ASSERT(reporter, kWidth == info3.fSize.fWidth && kHeight == info3.fSize.fHeight); |
| 903 | REPORTER_ASSERT(reporter, info3.fCTM.isIdentity()); |
| 904 | REPORTER_ASSERT(reporter, 0 == info3.fOffset.fX && 0 == info3.fOffset.fY); |
| 905 | REPORTER_ASSERT(reporter, NULL != info3.fPaint); |
| 906 | REPORTER_ASSERT(reporter, !info3.fIsNested && !info3.fHasNestedLayers); |
commit-bot@chromium.org | 0205aba | 2014-05-06 12:02:22 +0000 | [diff] [blame] | 907 | |
bsalomon | e904c09 | 2014-07-17 10:50:59 -0700 | [diff] [blame] | 908 | #if 0 // needs more though for GrGatherCanvas |
| 909 | REPORTER_ASSERT(reporter, !info4.fValid); // paint is/was uncopyable |
| 910 | REPORTER_ASSERT(reporter, kWidth == info4.fSize.fWidth && kHeight == info4.fSize.fHeight); |
| 911 | REPORTER_ASSERT(reporter, 0 == info4.fOffset.fX && 0 == info4.fOffset.fY); |
| 912 | REPORTER_ASSERT(reporter, info4.fCTM.isIdentity()); |
| 913 | REPORTER_ASSERT(reporter, NULL == info4.fPaint); // paint is/was uncopyable |
| 914 | REPORTER_ASSERT(reporter, !info4.fIsNested && !info4.fHasNestedLayers); |
| 915 | #endif |
| 916 | } |
commit-bot@chromium.org | 0205aba | 2014-05-06 12:02:22 +0000 | [diff] [blame] | 917 | } |
| 918 | } |
| 919 | |
robertphillips@google.com | b950c6f | 2014-04-25 00:02:12 +0000 | [diff] [blame] | 920 | #endif |
commit-bot@chromium.org | e2cb12a | 2014-04-24 21:53:13 +0000 | [diff] [blame] | 921 | |
commit-bot@chromium.org | ea7d08e | 2014-02-13 16:00:51 +0000 | [diff] [blame] | 922 | static void set_canvas_to_save_count_4(SkCanvas* canvas) { |
| 923 | canvas->restoreToCount(1); |
| 924 | canvas->save(); |
| 925 | canvas->save(); |
| 926 | canvas->save(); |
| 927 | } |
| 928 | |
commit-bot@chromium.org | 6d3eaea | 2014-05-27 23:41:45 +0000 | [diff] [blame] | 929 | /** |
| 930 | * A canvas that records the number of saves, saveLayers and restores. |
| 931 | */ |
| 932 | class SaveCountingCanvas : public SkCanvas { |
| 933 | public: |
| 934 | SaveCountingCanvas(int width, int height) |
| 935 | : INHERITED(width, height) |
| 936 | , fSaveCount(0) |
| 937 | , fSaveLayerCount(0) |
| 938 | , fRestoreCount(0){ |
| 939 | } |
| 940 | |
| 941 | virtual SaveLayerStrategy willSaveLayer(const SkRect* bounds, const SkPaint* paint, |
| 942 | SaveFlags flags) SK_OVERRIDE { |
| 943 | ++fSaveLayerCount; |
| 944 | return this->INHERITED::willSaveLayer(bounds, paint, flags); |
| 945 | } |
| 946 | |
Florin Malita | 5f6102d | 2014-06-30 10:13:28 -0400 | [diff] [blame] | 947 | virtual void willSave() SK_OVERRIDE { |
commit-bot@chromium.org | 6d3eaea | 2014-05-27 23:41:45 +0000 | [diff] [blame] | 948 | ++fSaveCount; |
Florin Malita | 5f6102d | 2014-06-30 10:13:28 -0400 | [diff] [blame] | 949 | this->INHERITED::willSave(); |
commit-bot@chromium.org | 6d3eaea | 2014-05-27 23:41:45 +0000 | [diff] [blame] | 950 | } |
| 951 | |
| 952 | virtual void willRestore() SK_OVERRIDE { |
| 953 | ++fRestoreCount; |
| 954 | this->INHERITED::willRestore(); |
| 955 | } |
| 956 | |
| 957 | unsigned int getSaveCount() const { return fSaveCount; } |
| 958 | unsigned int getSaveLayerCount() const { return fSaveLayerCount; } |
| 959 | unsigned int getRestoreCount() const { return fRestoreCount; } |
| 960 | |
| 961 | private: |
| 962 | unsigned int fSaveCount; |
| 963 | unsigned int fSaveLayerCount; |
| 964 | unsigned int fRestoreCount; |
| 965 | |
| 966 | typedef SkCanvas INHERITED; |
| 967 | }; |
| 968 | |
skia.committer@gmail.com | 8e7d37d | 2014-05-28 03:06:06 +0000 | [diff] [blame] | 969 | void check_save_state(skiatest::Reporter* reporter, SkPicture* picture, |
commit-bot@chromium.org | 6d3eaea | 2014-05-27 23:41:45 +0000 | [diff] [blame] | 970 | unsigned int numSaves, unsigned int numSaveLayers, |
| 971 | unsigned int numRestores) { |
| 972 | SaveCountingCanvas canvas(picture->width(), picture->height()); |
| 973 | |
| 974 | picture->draw(&canvas); |
| 975 | |
| 976 | REPORTER_ASSERT(reporter, numSaves == canvas.getSaveCount()); |
| 977 | REPORTER_ASSERT(reporter, numSaveLayers == canvas.getSaveLayerCount()); |
| 978 | REPORTER_ASSERT(reporter, numRestores == canvas.getRestoreCount()); |
| 979 | } |
| 980 | |
| 981 | // This class exists so SkPicture can friend it and give it access to |
| 982 | // the 'partialReplay' method. |
| 983 | class SkPictureRecorderReplayTester { |
| 984 | public: |
| 985 | static SkPicture* Copy(SkPictureRecorder* recorder) { |
| 986 | SkPictureRecorder recorder2; |
| 987 | |
robertphillips | 9f1c241 | 2014-06-09 06:25:34 -0700 | [diff] [blame] | 988 | SkCanvas* canvas = recorder2.beginRecording(10, 10); |
commit-bot@chromium.org | 6d3eaea | 2014-05-27 23:41:45 +0000 | [diff] [blame] | 989 | |
| 990 | recorder->partialReplay(canvas); |
| 991 | |
| 992 | return recorder2.endRecording(); |
| 993 | } |
| 994 | }; |
| 995 | |
robertphillips | 9058d60 | 2014-06-10 11:45:46 -0700 | [diff] [blame] | 996 | static void create_imbalance(SkCanvas* canvas) { |
| 997 | SkRect clipRect = SkRect::MakeWH(2, 2); |
| 998 | SkRect drawRect = SkRect::MakeWH(10, 10); |
| 999 | canvas->save(); |
| 1000 | canvas->clipRect(clipRect, SkRegion::kReplace_Op); |
| 1001 | canvas->translate(1.0f, 1.0f); |
| 1002 | SkPaint p; |
| 1003 | p.setColor(SK_ColorGREEN); |
| 1004 | canvas->drawRect(drawRect, p); |
| 1005 | // no restore |
| 1006 | } |
| 1007 | |
| 1008 | // This tests that replaying a potentially unbalanced picture into a canvas |
| 1009 | // doesn't affect the canvas' save count or matrix/clip state. |
| 1010 | static void check_balance(skiatest::Reporter* reporter, SkPicture* picture) { |
| 1011 | SkBitmap bm; |
| 1012 | bm.allocN32Pixels(4, 3); |
| 1013 | SkCanvas canvas(bm); |
| 1014 | |
| 1015 | int beforeSaveCount = canvas.getSaveCount(); |
| 1016 | |
| 1017 | SkMatrix beforeMatrix = canvas.getTotalMatrix(); |
| 1018 | |
| 1019 | SkRect beforeClip; |
| 1020 | |
| 1021 | canvas.getClipBounds(&beforeClip); |
| 1022 | |
| 1023 | canvas.drawPicture(picture); |
| 1024 | |
| 1025 | REPORTER_ASSERT(reporter, beforeSaveCount == canvas.getSaveCount()); |
| 1026 | REPORTER_ASSERT(reporter, beforeMatrix == canvas.getTotalMatrix()); |
| 1027 | |
| 1028 | SkRect afterClip; |
| 1029 | |
| 1030 | canvas.getClipBounds(&afterClip); |
| 1031 | |
| 1032 | REPORTER_ASSERT(reporter, afterClip == beforeClip); |
| 1033 | } |
| 1034 | |
commit-bot@chromium.org | 6d3eaea | 2014-05-27 23:41:45 +0000 | [diff] [blame] | 1035 | // Test out SkPictureRecorder::partialReplay |
| 1036 | DEF_TEST(PictureRecorder_replay, reporter) { |
| 1037 | // check save/saveLayer state |
| 1038 | { |
| 1039 | SkPictureRecorder recorder; |
| 1040 | |
robertphillips | 9f1c241 | 2014-06-09 06:25:34 -0700 | [diff] [blame] | 1041 | SkCanvas* canvas = recorder.beginRecording(10, 10); |
commit-bot@chromium.org | 6d3eaea | 2014-05-27 23:41:45 +0000 | [diff] [blame] | 1042 | |
| 1043 | canvas->saveLayer(NULL, NULL); |
| 1044 | |
| 1045 | SkAutoTUnref<SkPicture> copy(SkPictureRecorderReplayTester::Copy(&recorder)); |
| 1046 | |
| 1047 | // The extra save and restore comes from the Copy process. |
| 1048 | check_save_state(reporter, copy, 2, 1, 3); |
| 1049 | |
| 1050 | canvas->saveLayer(NULL, NULL); |
| 1051 | |
| 1052 | SkAutoTUnref<SkPicture> final(recorder.endRecording()); |
| 1053 | |
| 1054 | check_save_state(reporter, final, 1, 2, 3); |
| 1055 | |
| 1056 | // The copy shouldn't pick up any operations added after it was made |
| 1057 | check_save_state(reporter, copy, 2, 1, 3); |
| 1058 | } |
| 1059 | |
| 1060 | // (partially) check leakage of draw ops |
| 1061 | { |
| 1062 | SkPictureRecorder recorder; |
| 1063 | |
robertphillips | 9f1c241 | 2014-06-09 06:25:34 -0700 | [diff] [blame] | 1064 | SkCanvas* canvas = recorder.beginRecording(10, 10); |
commit-bot@chromium.org | 6d3eaea | 2014-05-27 23:41:45 +0000 | [diff] [blame] | 1065 | |
| 1066 | SkRect r = SkRect::MakeWH(5, 5); |
| 1067 | SkPaint p; |
| 1068 | |
| 1069 | canvas->drawRect(r, p); |
| 1070 | |
| 1071 | SkAutoTUnref<SkPicture> copy(SkPictureRecorderReplayTester::Copy(&recorder)); |
| 1072 | |
| 1073 | REPORTER_ASSERT(reporter, !copy->willPlayBackBitmaps()); |
| 1074 | |
| 1075 | SkBitmap bm; |
| 1076 | make_bm(&bm, 10, 10, SK_ColorRED, true); |
| 1077 | |
| 1078 | r.offset(5.0f, 5.0f); |
| 1079 | canvas->drawBitmapRectToRect(bm, NULL, r); |
| 1080 | |
| 1081 | SkAutoTUnref<SkPicture> final(recorder.endRecording()); |
| 1082 | REPORTER_ASSERT(reporter, final->willPlayBackBitmaps()); |
| 1083 | |
| 1084 | REPORTER_ASSERT(reporter, copy->uniqueID() != final->uniqueID()); |
| 1085 | |
| 1086 | // The snapshot shouldn't pick up any operations added after it was made |
| 1087 | REPORTER_ASSERT(reporter, !copy->willPlayBackBitmaps()); |
| 1088 | } |
robertphillips | 9058d60 | 2014-06-10 11:45:46 -0700 | [diff] [blame] | 1089 | |
| 1090 | // Recreate the Android partialReplay test case |
| 1091 | { |
| 1092 | SkPictureRecorder recorder; |
| 1093 | |
| 1094 | SkCanvas* canvas = recorder.beginRecording(4, 3, NULL, 0); |
| 1095 | create_imbalance(canvas); |
| 1096 | |
| 1097 | int expectedSaveCount = canvas->getSaveCount(); |
| 1098 | |
| 1099 | SkAutoTUnref<SkPicture> copy(SkPictureRecorderReplayTester::Copy(&recorder)); |
| 1100 | check_balance(reporter, copy); |
| 1101 | |
| 1102 | REPORTER_ASSERT(reporter, expectedSaveCount = canvas->getSaveCount()); |
| 1103 | |
| 1104 | // End the recording of source to test the picture finalization |
| 1105 | // process isn't complicated by the partialReplay step |
| 1106 | SkAutoTUnref<SkPicture> final(recorder.endRecording()); |
| 1107 | } |
commit-bot@chromium.org | 6d3eaea | 2014-05-27 23:41:45 +0000 | [diff] [blame] | 1108 | } |
commit-bot@chromium.org | 6d3eaea | 2014-05-27 23:41:45 +0000 | [diff] [blame] | 1109 | |
commit-bot@chromium.org | ea7d08e | 2014-02-13 16:00:51 +0000 | [diff] [blame] | 1110 | static void test_unbalanced_save_restores(skiatest::Reporter* reporter) { |
| 1111 | SkCanvas testCanvas(100, 100); |
| 1112 | set_canvas_to_save_count_4(&testCanvas); |
| 1113 | |
| 1114 | REPORTER_ASSERT(reporter, 4 == testCanvas.getSaveCount()); |
| 1115 | |
| 1116 | SkPaint paint; |
| 1117 | SkRect rect = SkRect::MakeLTRB(-10000000, -10000000, 10000000, 10000000); |
| 1118 | |
robertphillips@google.com | 84b18c7 | 2014-04-13 19:09:42 +0000 | [diff] [blame] | 1119 | SkPictureRecorder recorder; |
commit-bot@chromium.org | ea7d08e | 2014-02-13 16:00:51 +0000 | [diff] [blame] | 1120 | |
robertphillips@google.com | 84b18c7 | 2014-04-13 19:09:42 +0000 | [diff] [blame] | 1121 | { |
| 1122 | // Create picture with 2 unbalanced saves |
robertphillips | 9f1c241 | 2014-06-09 06:25:34 -0700 | [diff] [blame] | 1123 | SkCanvas* canvas = recorder.beginRecording(100, 100); |
robertphillips@google.com | 84b18c7 | 2014-04-13 19:09:42 +0000 | [diff] [blame] | 1124 | canvas->save(); |
| 1125 | canvas->translate(10, 10); |
| 1126 | canvas->drawRect(rect, paint); |
| 1127 | canvas->save(); |
| 1128 | canvas->translate(10, 10); |
| 1129 | canvas->drawRect(rect, paint); |
| 1130 | SkAutoTUnref<SkPicture> extraSavePicture(recorder.endRecording()); |
| 1131 | |
robertphillips | 9b14f26 | 2014-06-04 05:40:44 -0700 | [diff] [blame] | 1132 | testCanvas.drawPicture(extraSavePicture); |
robertphillips@google.com | 84b18c7 | 2014-04-13 19:09:42 +0000 | [diff] [blame] | 1133 | REPORTER_ASSERT(reporter, 4 == testCanvas.getSaveCount()); |
| 1134 | } |
commit-bot@chromium.org | ea7d08e | 2014-02-13 16:00:51 +0000 | [diff] [blame] | 1135 | |
| 1136 | set_canvas_to_save_count_4(&testCanvas); |
| 1137 | |
robertphillips@google.com | 84b18c7 | 2014-04-13 19:09:42 +0000 | [diff] [blame] | 1138 | { |
| 1139 | // Create picture with 2 unbalanced restores |
robertphillips | 9f1c241 | 2014-06-09 06:25:34 -0700 | [diff] [blame] | 1140 | SkCanvas* canvas = recorder.beginRecording(100, 100); |
robertphillips@google.com | 84b18c7 | 2014-04-13 19:09:42 +0000 | [diff] [blame] | 1141 | canvas->save(); |
| 1142 | canvas->translate(10, 10); |
| 1143 | canvas->drawRect(rect, paint); |
| 1144 | canvas->save(); |
| 1145 | canvas->translate(10, 10); |
| 1146 | canvas->drawRect(rect, paint); |
| 1147 | canvas->restore(); |
| 1148 | canvas->restore(); |
| 1149 | canvas->restore(); |
| 1150 | canvas->restore(); |
| 1151 | SkAutoTUnref<SkPicture> extraRestorePicture(recorder.endRecording()); |
commit-bot@chromium.org | ea7d08e | 2014-02-13 16:00:51 +0000 | [diff] [blame] | 1152 | |
robertphillips | 9b14f26 | 2014-06-04 05:40:44 -0700 | [diff] [blame] | 1153 | testCanvas.drawPicture(extraRestorePicture); |
robertphillips@google.com | 84b18c7 | 2014-04-13 19:09:42 +0000 | [diff] [blame] | 1154 | REPORTER_ASSERT(reporter, 4 == testCanvas.getSaveCount()); |
| 1155 | } |
commit-bot@chromium.org | ea7d08e | 2014-02-13 16:00:51 +0000 | [diff] [blame] | 1156 | |
robertphillips@google.com | 84b18c7 | 2014-04-13 19:09:42 +0000 | [diff] [blame] | 1157 | set_canvas_to_save_count_4(&testCanvas); |
commit-bot@chromium.org | ea7d08e | 2014-02-13 16:00:51 +0000 | [diff] [blame] | 1158 | |
robertphillips@google.com | 84b18c7 | 2014-04-13 19:09:42 +0000 | [diff] [blame] | 1159 | { |
robertphillips | 9f1c241 | 2014-06-09 06:25:34 -0700 | [diff] [blame] | 1160 | SkCanvas* canvas = recorder.beginRecording(100, 100); |
robertphillips@google.com | 84b18c7 | 2014-04-13 19:09:42 +0000 | [diff] [blame] | 1161 | canvas->translate(10, 10); |
| 1162 | canvas->drawRect(rect, paint); |
| 1163 | SkAutoTUnref<SkPicture> noSavePicture(recorder.endRecording()); |
| 1164 | |
robertphillips | 9b14f26 | 2014-06-04 05:40:44 -0700 | [diff] [blame] | 1165 | testCanvas.drawPicture(noSavePicture); |
robertphillips@google.com | 84b18c7 | 2014-04-13 19:09:42 +0000 | [diff] [blame] | 1166 | REPORTER_ASSERT(reporter, 4 == testCanvas.getSaveCount()); |
| 1167 | REPORTER_ASSERT(reporter, testCanvas.getTotalMatrix().isIdentity()); |
| 1168 | } |
commit-bot@chromium.org | ea7d08e | 2014-02-13 16:00:51 +0000 | [diff] [blame] | 1169 | } |
| 1170 | |
sugoi@google.com | 54f0d1b | 2013-02-27 19:17:41 +0000 | [diff] [blame] | 1171 | static void test_peephole() { |
commit-bot@chromium.org | e0e7cfe | 2013-09-09 20:09:12 +0000 | [diff] [blame] | 1172 | SkRandom rand; |
reed@google.com | 21b519d | 2012-10-02 17:42:15 +0000 | [diff] [blame] | 1173 | |
robertphillips@google.com | 84b18c7 | 2014-04-13 19:09:42 +0000 | [diff] [blame] | 1174 | SkPictureRecorder recorder; |
| 1175 | |
reed@google.com | 21b519d | 2012-10-02 17:42:15 +0000 | [diff] [blame] | 1176 | for (int j = 0; j < 100; j++) { |
commit-bot@chromium.org | e0e7cfe | 2013-09-09 20:09:12 +0000 | [diff] [blame] | 1177 | SkRandom rand2(rand); // remember the seed |
reed@google.com | 21b519d | 2012-10-02 17:42:15 +0000 | [diff] [blame] | 1178 | |
robertphillips | 9f1c241 | 2014-06-09 06:25:34 -0700 | [diff] [blame] | 1179 | SkCanvas* canvas = recorder.beginRecording(100, 100); |
reed@google.com | 21b519d | 2012-10-02 17:42:15 +0000 | [diff] [blame] | 1180 | |
| 1181 | for (int i = 0; i < 1000; ++i) { |
| 1182 | rand_op(canvas, rand); |
| 1183 | } |
robertphillips@google.com | 84b18c7 | 2014-04-13 19:09:42 +0000 | [diff] [blame] | 1184 | SkAutoTUnref<SkPicture> picture(recorder.endRecording()); |
jvanverth@google.com | c490f80 | 2013-03-04 13:56:38 +0000 | [diff] [blame] | 1185 | |
| 1186 | rand = rand2; |
reed@google.com | 21b519d | 2012-10-02 17:42:15 +0000 | [diff] [blame] | 1187 | } |
| 1188 | |
| 1189 | { |
robertphillips | 9f1c241 | 2014-06-09 06:25:34 -0700 | [diff] [blame] | 1190 | SkCanvas* canvas = recorder.beginRecording(100, 100); |
reed@google.com | 21b519d | 2012-10-02 17:42:15 +0000 | [diff] [blame] | 1191 | SkRect rect = SkRect::MakeWH(50, 50); |
skia.committer@gmail.com | 52c2437 | 2012-10-03 02:01:13 +0000 | [diff] [blame] | 1192 | |
reed@google.com | 21b519d | 2012-10-02 17:42:15 +0000 | [diff] [blame] | 1193 | for (int i = 0; i < 100; ++i) { |
| 1194 | canvas->save(); |
| 1195 | } |
| 1196 | while (canvas->getSaveCount() > 1) { |
| 1197 | canvas->clipRect(rect); |
| 1198 | canvas->restore(); |
| 1199 | } |
robertphillips@google.com | 84b18c7 | 2014-04-13 19:09:42 +0000 | [diff] [blame] | 1200 | SkAutoTUnref<SkPicture> picture(recorder.endRecording()); |
reed@google.com | 21b519d | 2012-10-02 17:42:15 +0000 | [diff] [blame] | 1201 | } |
| 1202 | } |
| 1203 | |
scroggo@google.com | 4b90b11 | 2012-12-04 15:08:56 +0000 | [diff] [blame] | 1204 | #ifndef SK_DEBUG |
| 1205 | // Only test this is in release mode. We deliberately crash in debug mode, since a valid caller |
| 1206 | // should never do this. |
| 1207 | static void test_bad_bitmap() { |
| 1208 | // This bitmap has a width and height but no pixels. As a result, attempting to record it will |
| 1209 | // fail. |
| 1210 | SkBitmap bm; |
commit-bot@chromium.org | a3264e5 | 2014-05-30 13:26:10 +0000 | [diff] [blame] | 1211 | bm.setInfo(SkImageInfo::MakeN32Premul(100, 100)); |
robertphillips@google.com | 84b18c7 | 2014-04-13 19:09:42 +0000 | [diff] [blame] | 1212 | SkPictureRecorder recorder; |
robertphillips | 9f1c241 | 2014-06-09 06:25:34 -0700 | [diff] [blame] | 1213 | SkCanvas* recordingCanvas = recorder.beginRecording(100, 100); |
scroggo@google.com | 4b90b11 | 2012-12-04 15:08:56 +0000 | [diff] [blame] | 1214 | recordingCanvas->drawBitmap(bm, 0, 0); |
robertphillips@google.com | 84b18c7 | 2014-04-13 19:09:42 +0000 | [diff] [blame] | 1215 | SkAutoTUnref<SkPicture> picture(recorder.endRecording()); |
scroggo@google.com | 4b90b11 | 2012-12-04 15:08:56 +0000 | [diff] [blame] | 1216 | |
| 1217 | SkCanvas canvas; |
robertphillips | 9b14f26 | 2014-06-04 05:40:44 -0700 | [diff] [blame] | 1218 | canvas.drawPicture(picture); |
scroggo@google.com | 4b90b11 | 2012-12-04 15:08:56 +0000 | [diff] [blame] | 1219 | } |
| 1220 | #endif |
| 1221 | |
reed@google.com | 672588b | 2014-01-08 15:42:01 +0000 | [diff] [blame] | 1222 | static SkData* encode_bitmap_to_data(size_t*, const SkBitmap& bm) { |
scroggo@google.com | 1b1bcc3 | 2013-05-21 20:31:23 +0000 | [diff] [blame] | 1223 | return SkImageEncoder::EncodeData(bm, SkImageEncoder::kPNG_Type, 100); |
scroggo@google.com | 7c9d539 | 2012-12-10 15:40:55 +0000 | [diff] [blame] | 1224 | } |
| 1225 | |
| 1226 | static SkData* serialized_picture_from_bitmap(const SkBitmap& bitmap) { |
robertphillips@google.com | 84b18c7 | 2014-04-13 19:09:42 +0000 | [diff] [blame] | 1227 | SkPictureRecorder recorder; |
robertphillips | 9f1c241 | 2014-06-09 06:25:34 -0700 | [diff] [blame] | 1228 | SkCanvas* canvas = recorder.beginRecording(bitmap.width(), bitmap.height()); |
scroggo@google.com | 7c9d539 | 2012-12-10 15:40:55 +0000 | [diff] [blame] | 1229 | canvas->drawBitmap(bitmap, 0, 0); |
robertphillips@google.com | 84b18c7 | 2014-04-13 19:09:42 +0000 | [diff] [blame] | 1230 | SkAutoTUnref<SkPicture> picture(recorder.endRecording()); |
| 1231 | |
scroggo@google.com | 7c9d539 | 2012-12-10 15:40:55 +0000 | [diff] [blame] | 1232 | SkDynamicMemoryWStream wStream; |
robertphillips@google.com | 84b18c7 | 2014-04-13 19:09:42 +0000 | [diff] [blame] | 1233 | picture->serialize(&wStream, &encode_bitmap_to_data); |
scroggo@google.com | 7c9d539 | 2012-12-10 15:40:55 +0000 | [diff] [blame] | 1234 | return wStream.copyToData(); |
| 1235 | } |
| 1236 | |
scroggo@google.com | 49ce11b | 2013-04-25 18:29:32 +0000 | [diff] [blame] | 1237 | struct ErrorContext { |
| 1238 | int fErrors; |
| 1239 | skiatest::Reporter* fReporter; |
| 1240 | }; |
| 1241 | |
| 1242 | static void assert_one_parse_error_cb(SkError error, void* context) { |
| 1243 | ErrorContext* errorContext = static_cast<ErrorContext*>(context); |
| 1244 | errorContext->fErrors++; |
| 1245 | // This test only expects one error, and that is a kParseError. If there are others, |
| 1246 | // there is some unknown problem. |
| 1247 | REPORTER_ASSERT_MESSAGE(errorContext->fReporter, 1 == errorContext->fErrors, |
| 1248 | "This threw more errors than expected."); |
| 1249 | REPORTER_ASSERT_MESSAGE(errorContext->fReporter, kParseError_SkError == error, |
| 1250 | SkGetLastErrorString()); |
| 1251 | } |
| 1252 | |
scroggo@google.com | 7c9d539 | 2012-12-10 15:40:55 +0000 | [diff] [blame] | 1253 | static void test_bitmap_with_encoded_data(skiatest::Reporter* reporter) { |
| 1254 | // Create a bitmap that will be encoded. |
| 1255 | SkBitmap original; |
| 1256 | make_bm(&original, 100, 100, SK_ColorBLUE, true); |
| 1257 | SkDynamicMemoryWStream wStream; |
| 1258 | if (!SkImageEncoder::EncodeStream(&wStream, original, SkImageEncoder::kPNG_Type, 100)) { |
| 1259 | return; |
| 1260 | } |
| 1261 | SkAutoDataUnref data(wStream.copyToData()); |
scroggo@google.com | 7c9d539 | 2012-12-10 15:40:55 +0000 | [diff] [blame] | 1262 | |
scroggo@google.com | 7c9d539 | 2012-12-10 15:40:55 +0000 | [diff] [blame] | 1263 | SkBitmap bm; |
halcanary@google.com | 3d50ea1 | 2014-01-02 13:15:13 +0000 | [diff] [blame] | 1264 | bool installSuccess = SkInstallDiscardablePixelRef( |
commit-bot@chromium.org | 2d970b5 | 2014-05-27 14:14:22 +0000 | [diff] [blame] | 1265 | SkDecodingImageGenerator::Create(data, SkDecodingImageGenerator::Options()), &bm); |
reed@google.com | bf79023 | 2013-12-13 19:45:58 +0000 | [diff] [blame] | 1266 | REPORTER_ASSERT(reporter, installSuccess); |
scroggo@google.com | 7c9d539 | 2012-12-10 15:40:55 +0000 | [diff] [blame] | 1267 | |
| 1268 | // Write both bitmaps to pictures, and ensure that the resulting data streams are the same. |
| 1269 | // Flattening original will follow the old path of performing an encode, while flattening bm |
| 1270 | // will use the already encoded data. |
| 1271 | SkAutoDataUnref picture1(serialized_picture_from_bitmap(original)); |
| 1272 | SkAutoDataUnref picture2(serialized_picture_from_bitmap(bm)); |
| 1273 | REPORTER_ASSERT(reporter, picture1->equals(picture2)); |
scroggo@google.com | 49ce11b | 2013-04-25 18:29:32 +0000 | [diff] [blame] | 1274 | // Now test that a parse error was generated when trying to create a new SkPicture without |
| 1275 | // providing a function to decode the bitmap. |
| 1276 | ErrorContext context; |
| 1277 | context.fErrors = 0; |
| 1278 | context.fReporter = reporter; |
| 1279 | SkSetErrorCallback(assert_one_parse_error_cb, &context); |
| 1280 | SkMemoryStream pictureStream(picture1); |
scroggo@google.com | 49ce11b | 2013-04-25 18:29:32 +0000 | [diff] [blame] | 1281 | SkClearLastError(); |
scroggo@google.com | f1754ec | 2013-06-28 21:32:00 +0000 | [diff] [blame] | 1282 | SkAutoUnref pictureFromStream(SkPicture::CreateFromStream(&pictureStream, NULL)); |
| 1283 | REPORTER_ASSERT(reporter, pictureFromStream.get() != NULL); |
scroggo@google.com | 49ce11b | 2013-04-25 18:29:32 +0000 | [diff] [blame] | 1284 | SkClearLastError(); |
| 1285 | SkSetErrorCallback(NULL, NULL); |
scroggo@google.com | 7c9d539 | 2012-12-10 15:40:55 +0000 | [diff] [blame] | 1286 | } |
| 1287 | |
commit-bot@chromium.org | 70512af | 2014-03-18 17:45:32 +0000 | [diff] [blame] | 1288 | static void test_draw_empty(skiatest::Reporter* reporter) { |
| 1289 | SkBitmap result; |
| 1290 | make_bm(&result, 2, 2, SK_ColorBLACK, false); |
| 1291 | |
| 1292 | SkCanvas canvas(result); |
| 1293 | |
| 1294 | { |
| 1295 | // stock SkPicture |
robertphillips@google.com | 84b18c7 | 2014-04-13 19:09:42 +0000 | [diff] [blame] | 1296 | SkPictureRecorder recorder; |
robertphillips | 9f1c241 | 2014-06-09 06:25:34 -0700 | [diff] [blame] | 1297 | recorder.beginRecording(1, 1); |
robertphillips@google.com | 84b18c7 | 2014-04-13 19:09:42 +0000 | [diff] [blame] | 1298 | SkAutoTUnref<SkPicture> picture(recorder.endRecording()); |
commit-bot@chromium.org | 70512af | 2014-03-18 17:45:32 +0000 | [diff] [blame] | 1299 | |
robertphillips | 9b14f26 | 2014-06-04 05:40:44 -0700 | [diff] [blame] | 1300 | canvas.drawPicture(picture); |
commit-bot@chromium.org | 70512af | 2014-03-18 17:45:32 +0000 | [diff] [blame] | 1301 | } |
| 1302 | |
| 1303 | { |
| 1304 | // tile grid |
commit-bot@chromium.org | 5fb2ce3 | 2014-04-17 23:35:06 +0000 | [diff] [blame] | 1305 | SkTileGridFactory::TileGridInfo gridInfo; |
commit-bot@chromium.org | 70512af | 2014-03-18 17:45:32 +0000 | [diff] [blame] | 1306 | gridInfo.fMargin.setEmpty(); |
| 1307 | gridInfo.fOffset.setZero(); |
| 1308 | gridInfo.fTileInterval.set(1, 1); |
| 1309 | |
commit-bot@chromium.org | 5fb2ce3 | 2014-04-17 23:35:06 +0000 | [diff] [blame] | 1310 | SkTileGridFactory factory(gridInfo); |
| 1311 | SkPictureRecorder recorder; |
robertphillips | 9f1c241 | 2014-06-09 06:25:34 -0700 | [diff] [blame] | 1312 | recorder.beginRecording(1, 1, &factory); |
robertphillips@google.com | 84b18c7 | 2014-04-13 19:09:42 +0000 | [diff] [blame] | 1313 | SkAutoTUnref<SkPicture> picture(recorder.endRecording()); |
commit-bot@chromium.org | 70512af | 2014-03-18 17:45:32 +0000 | [diff] [blame] | 1314 | |
robertphillips | 9b14f26 | 2014-06-04 05:40:44 -0700 | [diff] [blame] | 1315 | canvas.drawPicture(picture); |
commit-bot@chromium.org | 70512af | 2014-03-18 17:45:32 +0000 | [diff] [blame] | 1316 | } |
| 1317 | |
| 1318 | { |
| 1319 | // RTree |
commit-bot@chromium.org | 5fb2ce3 | 2014-04-17 23:35:06 +0000 | [diff] [blame] | 1320 | SkRTreeFactory factory; |
| 1321 | SkPictureRecorder recorder; |
robertphillips | 9f1c241 | 2014-06-09 06:25:34 -0700 | [diff] [blame] | 1322 | recorder.beginRecording(1, 1, &factory); |
robertphillips@google.com | 84b18c7 | 2014-04-13 19:09:42 +0000 | [diff] [blame] | 1323 | SkAutoTUnref<SkPicture> picture(recorder.endRecording()); |
commit-bot@chromium.org | 70512af | 2014-03-18 17:45:32 +0000 | [diff] [blame] | 1324 | |
robertphillips | 9b14f26 | 2014-06-04 05:40:44 -0700 | [diff] [blame] | 1325 | canvas.drawPicture(picture); |
commit-bot@chromium.org | 70512af | 2014-03-18 17:45:32 +0000 | [diff] [blame] | 1326 | } |
| 1327 | |
| 1328 | { |
| 1329 | // quad tree |
commit-bot@chromium.org | 5fb2ce3 | 2014-04-17 23:35:06 +0000 | [diff] [blame] | 1330 | SkQuadTreeFactory factory; |
| 1331 | SkPictureRecorder recorder; |
robertphillips | 9f1c241 | 2014-06-09 06:25:34 -0700 | [diff] [blame] | 1332 | recorder.beginRecording(1, 1, &factory); |
robertphillips@google.com | 84b18c7 | 2014-04-13 19:09:42 +0000 | [diff] [blame] | 1333 | SkAutoTUnref<SkPicture> picture(recorder.endRecording()); |
commit-bot@chromium.org | 70512af | 2014-03-18 17:45:32 +0000 | [diff] [blame] | 1334 | |
robertphillips | 9b14f26 | 2014-06-04 05:40:44 -0700 | [diff] [blame] | 1335 | canvas.drawPicture(picture); |
commit-bot@chromium.org | 70512af | 2014-03-18 17:45:32 +0000 | [diff] [blame] | 1336 | } |
| 1337 | } |
| 1338 | |
junov@chromium.org | d575eed | 2013-05-08 15:39:13 +0000 | [diff] [blame] | 1339 | static void test_clip_bound_opt(skiatest::Reporter* reporter) { |
| 1340 | // Test for crbug.com/229011 |
| 1341 | SkRect rect1 = SkRect::MakeXYWH(SkIntToScalar(4), SkIntToScalar(4), |
| 1342 | SkIntToScalar(2), SkIntToScalar(2)); |
| 1343 | SkRect rect2 = SkRect::MakeXYWH(SkIntToScalar(7), SkIntToScalar(7), |
| 1344 | SkIntToScalar(1), SkIntToScalar(1)); |
| 1345 | SkRect rect3 = SkRect::MakeXYWH(SkIntToScalar(6), SkIntToScalar(6), |
| 1346 | SkIntToScalar(1), SkIntToScalar(1)); |
| 1347 | |
| 1348 | SkPath invPath; |
| 1349 | invPath.addOval(rect1); |
| 1350 | invPath.setFillType(SkPath::kInverseEvenOdd_FillType); |
| 1351 | SkPath path; |
| 1352 | path.addOval(rect2); |
| 1353 | SkPath path2; |
| 1354 | path2.addOval(rect3); |
| 1355 | SkIRect clipBounds; |
robertphillips@google.com | 84b18c7 | 2014-04-13 19:09:42 +0000 | [diff] [blame] | 1356 | SkPictureRecorder recorder; |
junov@chromium.org | d575eed | 2013-05-08 15:39:13 +0000 | [diff] [blame] | 1357 | // Minimalist test set for 100% code coverage of |
| 1358 | // SkPictureRecord::updateClipConservativelyUsingBounds |
| 1359 | { |
robertphillips | 9f1c241 | 2014-06-09 06:25:34 -0700 | [diff] [blame] | 1360 | SkCanvas* canvas = recorder.beginRecording(10, 10); |
junov@chromium.org | d575eed | 2013-05-08 15:39:13 +0000 | [diff] [blame] | 1361 | canvas->clipPath(invPath, SkRegion::kIntersect_Op); |
| 1362 | bool nonEmpty = canvas->getClipDeviceBounds(&clipBounds); |
| 1363 | REPORTER_ASSERT(reporter, true == nonEmpty); |
| 1364 | REPORTER_ASSERT(reporter, 0 == clipBounds.fLeft); |
| 1365 | REPORTER_ASSERT(reporter, 0 == clipBounds.fTop); |
| 1366 | REPORTER_ASSERT(reporter, 10 == clipBounds.fBottom); |
| 1367 | REPORTER_ASSERT(reporter, 10 == clipBounds.fRight); |
| 1368 | } |
| 1369 | { |
robertphillips | 9f1c241 | 2014-06-09 06:25:34 -0700 | [diff] [blame] | 1370 | SkCanvas* canvas = recorder.beginRecording(10, 10); |
junov@chromium.org | d575eed | 2013-05-08 15:39:13 +0000 | [diff] [blame] | 1371 | canvas->clipPath(path, SkRegion::kIntersect_Op); |
| 1372 | canvas->clipPath(invPath, SkRegion::kIntersect_Op); |
| 1373 | bool nonEmpty = canvas->getClipDeviceBounds(&clipBounds); |
| 1374 | REPORTER_ASSERT(reporter, true == nonEmpty); |
| 1375 | REPORTER_ASSERT(reporter, 7 == clipBounds.fLeft); |
| 1376 | REPORTER_ASSERT(reporter, 7 == clipBounds.fTop); |
| 1377 | REPORTER_ASSERT(reporter, 8 == clipBounds.fBottom); |
| 1378 | REPORTER_ASSERT(reporter, 8 == clipBounds.fRight); |
| 1379 | } |
| 1380 | { |
robertphillips | 9f1c241 | 2014-06-09 06:25:34 -0700 | [diff] [blame] | 1381 | SkCanvas* canvas = recorder.beginRecording(10, 10); |
junov@chromium.org | d575eed | 2013-05-08 15:39:13 +0000 | [diff] [blame] | 1382 | canvas->clipPath(path, SkRegion::kIntersect_Op); |
| 1383 | canvas->clipPath(invPath, SkRegion::kUnion_Op); |
| 1384 | bool nonEmpty = canvas->getClipDeviceBounds(&clipBounds); |
| 1385 | REPORTER_ASSERT(reporter, true == nonEmpty); |
| 1386 | REPORTER_ASSERT(reporter, 0 == clipBounds.fLeft); |
| 1387 | REPORTER_ASSERT(reporter, 0 == clipBounds.fTop); |
| 1388 | REPORTER_ASSERT(reporter, 10 == clipBounds.fBottom); |
| 1389 | REPORTER_ASSERT(reporter, 10 == clipBounds.fRight); |
| 1390 | } |
| 1391 | { |
robertphillips | 9f1c241 | 2014-06-09 06:25:34 -0700 | [diff] [blame] | 1392 | SkCanvas* canvas = recorder.beginRecording(10, 10); |
junov@chromium.org | d575eed | 2013-05-08 15:39:13 +0000 | [diff] [blame] | 1393 | canvas->clipPath(path, SkRegion::kDifference_Op); |
| 1394 | bool nonEmpty = canvas->getClipDeviceBounds(&clipBounds); |
| 1395 | REPORTER_ASSERT(reporter, true == nonEmpty); |
| 1396 | REPORTER_ASSERT(reporter, 0 == clipBounds.fLeft); |
| 1397 | REPORTER_ASSERT(reporter, 0 == clipBounds.fTop); |
| 1398 | REPORTER_ASSERT(reporter, 10 == clipBounds.fBottom); |
| 1399 | REPORTER_ASSERT(reporter, 10 == clipBounds.fRight); |
| 1400 | } |
| 1401 | { |
robertphillips | 9f1c241 | 2014-06-09 06:25:34 -0700 | [diff] [blame] | 1402 | SkCanvas* canvas = recorder.beginRecording(10, 10); |
junov@chromium.org | d575eed | 2013-05-08 15:39:13 +0000 | [diff] [blame] | 1403 | canvas->clipPath(path, SkRegion::kReverseDifference_Op); |
| 1404 | bool nonEmpty = canvas->getClipDeviceBounds(&clipBounds); |
| 1405 | // True clip is actually empty in this case, but the best |
| 1406 | // determination we can make using only bounds as input is that the |
| 1407 | // clip is included in the bounds of 'path'. |
| 1408 | REPORTER_ASSERT(reporter, true == nonEmpty); |
| 1409 | REPORTER_ASSERT(reporter, 7 == clipBounds.fLeft); |
| 1410 | REPORTER_ASSERT(reporter, 7 == clipBounds.fTop); |
| 1411 | REPORTER_ASSERT(reporter, 8 == clipBounds.fBottom); |
| 1412 | REPORTER_ASSERT(reporter, 8 == clipBounds.fRight); |
| 1413 | } |
| 1414 | { |
robertphillips | 9f1c241 | 2014-06-09 06:25:34 -0700 | [diff] [blame] | 1415 | SkCanvas* canvas = recorder.beginRecording(10, 10); |
junov@chromium.org | d575eed | 2013-05-08 15:39:13 +0000 | [diff] [blame] | 1416 | canvas->clipPath(path, SkRegion::kIntersect_Op); |
| 1417 | canvas->clipPath(path2, SkRegion::kXOR_Op); |
| 1418 | bool nonEmpty = canvas->getClipDeviceBounds(&clipBounds); |
| 1419 | REPORTER_ASSERT(reporter, true == nonEmpty); |
| 1420 | REPORTER_ASSERT(reporter, 6 == clipBounds.fLeft); |
| 1421 | REPORTER_ASSERT(reporter, 6 == clipBounds.fTop); |
| 1422 | REPORTER_ASSERT(reporter, 8 == clipBounds.fBottom); |
| 1423 | REPORTER_ASSERT(reporter, 8 == clipBounds.fRight); |
| 1424 | } |
| 1425 | } |
| 1426 | |
fmalita@google.com | d0f1a4f | 2013-08-27 15:50:19 +0000 | [diff] [blame] | 1427 | /** |
| 1428 | * A canvas that records the number of clip commands. |
| 1429 | */ |
| 1430 | class ClipCountingCanvas : public SkCanvas { |
| 1431 | public: |
commit-bot@chromium.org | 6d3eaea | 2014-05-27 23:41:45 +0000 | [diff] [blame] | 1432 | ClipCountingCanvas(int width, int height) |
commit-bot@chromium.org | e254310 | 2014-01-31 19:42:58 +0000 | [diff] [blame] | 1433 | : INHERITED(width, height) |
fmalita@google.com | d0f1a4f | 2013-08-27 15:50:19 +0000 | [diff] [blame] | 1434 | , fClipCount(0){ |
| 1435 | } |
| 1436 | |
skia.committer@gmail.com | 370a899 | 2014-03-01 03:02:09 +0000 | [diff] [blame] | 1437 | virtual void onClipRect(const SkRect& r, |
| 1438 | SkRegion::Op op, |
robertphillips@google.com | 8f90a89 | 2014-02-28 18:19:39 +0000 | [diff] [blame] | 1439 | ClipEdgeStyle edgeStyle) SK_OVERRIDE { |
fmalita@google.com | d0f1a4f | 2013-08-27 15:50:19 +0000 | [diff] [blame] | 1440 | fClipCount += 1; |
robertphillips@google.com | 8f90a89 | 2014-02-28 18:19:39 +0000 | [diff] [blame] | 1441 | this->INHERITED::onClipRect(r, op, edgeStyle); |
fmalita@google.com | d0f1a4f | 2013-08-27 15:50:19 +0000 | [diff] [blame] | 1442 | } |
| 1443 | |
skia.committer@gmail.com | 370a899 | 2014-03-01 03:02:09 +0000 | [diff] [blame] | 1444 | virtual void onClipRRect(const SkRRect& rrect, |
| 1445 | SkRegion::Op op, |
robertphillips@google.com | 8f90a89 | 2014-02-28 18:19:39 +0000 | [diff] [blame] | 1446 | ClipEdgeStyle edgeStyle)SK_OVERRIDE { |
fmalita@google.com | d0f1a4f | 2013-08-27 15:50:19 +0000 | [diff] [blame] | 1447 | fClipCount += 1; |
robertphillips@google.com | 8f90a89 | 2014-02-28 18:19:39 +0000 | [diff] [blame] | 1448 | this->INHERITED::onClipRRect(rrect, op, edgeStyle); |
fmalita@google.com | d0f1a4f | 2013-08-27 15:50:19 +0000 | [diff] [blame] | 1449 | } |
| 1450 | |
skia.committer@gmail.com | 370a899 | 2014-03-01 03:02:09 +0000 | [diff] [blame] | 1451 | virtual void onClipPath(const SkPath& path, |
| 1452 | SkRegion::Op op, |
robertphillips@google.com | 8f90a89 | 2014-02-28 18:19:39 +0000 | [diff] [blame] | 1453 | ClipEdgeStyle edgeStyle) SK_OVERRIDE { |
fmalita@google.com | d0f1a4f | 2013-08-27 15:50:19 +0000 | [diff] [blame] | 1454 | fClipCount += 1; |
robertphillips@google.com | 8f90a89 | 2014-02-28 18:19:39 +0000 | [diff] [blame] | 1455 | this->INHERITED::onClipPath(path, op, edgeStyle); |
| 1456 | } |
| 1457 | |
| 1458 | virtual void onClipRegion(const SkRegion& deviceRgn, SkRegion::Op op) SK_OVERRIDE { |
| 1459 | fClipCount += 1; |
| 1460 | this->INHERITED::onClipRegion(deviceRgn, op); |
fmalita@google.com | d0f1a4f | 2013-08-27 15:50:19 +0000 | [diff] [blame] | 1461 | } |
| 1462 | |
| 1463 | unsigned getClipCount() const { return fClipCount; } |
| 1464 | |
| 1465 | private: |
| 1466 | unsigned fClipCount; |
| 1467 | |
| 1468 | typedef SkCanvas INHERITED; |
| 1469 | }; |
| 1470 | |
| 1471 | static void test_clip_expansion(skiatest::Reporter* reporter) { |
robertphillips@google.com | 84b18c7 | 2014-04-13 19:09:42 +0000 | [diff] [blame] | 1472 | SkPictureRecorder recorder; |
robertphillips | 9f1c241 | 2014-06-09 06:25:34 -0700 | [diff] [blame] | 1473 | SkCanvas* canvas = recorder.beginRecording(10, 10); |
fmalita@google.com | d0f1a4f | 2013-08-27 15:50:19 +0000 | [diff] [blame] | 1474 | |
| 1475 | canvas->clipRect(SkRect::MakeEmpty(), SkRegion::kReplace_Op); |
| 1476 | // The following expanding clip should not be skipped. |
| 1477 | canvas->clipRect(SkRect::MakeXYWH(4, 4, 3, 3), SkRegion::kUnion_Op); |
| 1478 | // Draw something so the optimizer doesn't just fold the world. |
| 1479 | SkPaint p; |
| 1480 | p.setColor(SK_ColorBLUE); |
| 1481 | canvas->drawPaint(p); |
robertphillips@google.com | 84b18c7 | 2014-04-13 19:09:42 +0000 | [diff] [blame] | 1482 | SkAutoTUnref<SkPicture> picture(recorder.endRecording()); |
fmalita@google.com | d0f1a4f | 2013-08-27 15:50:19 +0000 | [diff] [blame] | 1483 | |
commit-bot@chromium.org | e254310 | 2014-01-31 19:42:58 +0000 | [diff] [blame] | 1484 | ClipCountingCanvas testCanvas(10, 10); |
robertphillips@google.com | 84b18c7 | 2014-04-13 19:09:42 +0000 | [diff] [blame] | 1485 | picture->draw(&testCanvas); |
fmalita@google.com | d0f1a4f | 2013-08-27 15:50:19 +0000 | [diff] [blame] | 1486 | |
| 1487 | // Both clips should be present on playback. |
| 1488 | REPORTER_ASSERT(reporter, testCanvas.getClipCount() == 2); |
| 1489 | } |
| 1490 | |
tomhudson@google.com | 381010e | 2013-10-24 11:12:47 +0000 | [diff] [blame] | 1491 | static void test_hierarchical(skiatest::Reporter* reporter) { |
| 1492 | SkBitmap bm; |
| 1493 | make_bm(&bm, 10, 10, SK_ColorRED, true); |
| 1494 | |
robertphillips@google.com | 84b18c7 | 2014-04-13 19:09:42 +0000 | [diff] [blame] | 1495 | SkPictureRecorder recorder; |
tomhudson@google.com | 381010e | 2013-10-24 11:12:47 +0000 | [diff] [blame] | 1496 | |
robertphillips | 9f1c241 | 2014-06-09 06:25:34 -0700 | [diff] [blame] | 1497 | recorder.beginRecording(10, 10); |
robertphillips@google.com | 84b18c7 | 2014-04-13 19:09:42 +0000 | [diff] [blame] | 1498 | SkAutoTUnref<SkPicture> childPlain(recorder.endRecording()); |
| 1499 | REPORTER_ASSERT(reporter, !childPlain->willPlayBackBitmaps()); // 0 |
tomhudson@google.com | 381010e | 2013-10-24 11:12:47 +0000 | [diff] [blame] | 1500 | |
robertphillips | 9f1c241 | 2014-06-09 06:25:34 -0700 | [diff] [blame] | 1501 | recorder.beginRecording(10, 10)->drawBitmap(bm, 0, 0); |
robertphillips@google.com | 84b18c7 | 2014-04-13 19:09:42 +0000 | [diff] [blame] | 1502 | SkAutoTUnref<SkPicture> childWithBitmap(recorder.endRecording()); |
| 1503 | REPORTER_ASSERT(reporter, childWithBitmap->willPlayBackBitmaps()); // 1 |
tomhudson@google.com | 381010e | 2013-10-24 11:12:47 +0000 | [diff] [blame] | 1504 | |
robertphillips@google.com | 84b18c7 | 2014-04-13 19:09:42 +0000 | [diff] [blame] | 1505 | { |
robertphillips | 9f1c241 | 2014-06-09 06:25:34 -0700 | [diff] [blame] | 1506 | SkCanvas* canvas = recorder.beginRecording(10, 10); |
robertphillips | 9b14f26 | 2014-06-04 05:40:44 -0700 | [diff] [blame] | 1507 | canvas->drawPicture(childPlain); |
robertphillips@google.com | 84b18c7 | 2014-04-13 19:09:42 +0000 | [diff] [blame] | 1508 | SkAutoTUnref<SkPicture> parentPP(recorder.endRecording()); |
| 1509 | REPORTER_ASSERT(reporter, !parentPP->willPlayBackBitmaps()); // 0 |
| 1510 | } |
| 1511 | { |
robertphillips | 9f1c241 | 2014-06-09 06:25:34 -0700 | [diff] [blame] | 1512 | SkCanvas* canvas = recorder.beginRecording(10, 10); |
robertphillips | 9b14f26 | 2014-06-04 05:40:44 -0700 | [diff] [blame] | 1513 | canvas->drawPicture(childWithBitmap); |
robertphillips@google.com | 84b18c7 | 2014-04-13 19:09:42 +0000 | [diff] [blame] | 1514 | SkAutoTUnref<SkPicture> parentPWB(recorder.endRecording()); |
| 1515 | REPORTER_ASSERT(reporter, parentPWB->willPlayBackBitmaps()); // 1 |
| 1516 | } |
| 1517 | { |
robertphillips | 9f1c241 | 2014-06-09 06:25:34 -0700 | [diff] [blame] | 1518 | SkCanvas* canvas = recorder.beginRecording(10, 10); |
robertphillips@google.com | 84b18c7 | 2014-04-13 19:09:42 +0000 | [diff] [blame] | 1519 | canvas->drawBitmap(bm, 0, 0); |
robertphillips | 9b14f26 | 2014-06-04 05:40:44 -0700 | [diff] [blame] | 1520 | canvas->drawPicture(childPlain); |
robertphillips@google.com | 84b18c7 | 2014-04-13 19:09:42 +0000 | [diff] [blame] | 1521 | SkAutoTUnref<SkPicture> parentWBP(recorder.endRecording()); |
| 1522 | REPORTER_ASSERT(reporter, parentWBP->willPlayBackBitmaps()); // 1 |
| 1523 | } |
| 1524 | { |
robertphillips | 9f1c241 | 2014-06-09 06:25:34 -0700 | [diff] [blame] | 1525 | SkCanvas* canvas = recorder.beginRecording(10, 10); |
robertphillips@google.com | 84b18c7 | 2014-04-13 19:09:42 +0000 | [diff] [blame] | 1526 | canvas->drawBitmap(bm, 0, 0); |
robertphillips | 9b14f26 | 2014-06-04 05:40:44 -0700 | [diff] [blame] | 1527 | canvas->drawPicture(childWithBitmap); |
robertphillips@google.com | 84b18c7 | 2014-04-13 19:09:42 +0000 | [diff] [blame] | 1528 | SkAutoTUnref<SkPicture> parentWBWB(recorder.endRecording()); |
| 1529 | REPORTER_ASSERT(reporter, parentWBWB->willPlayBackBitmaps()); // 2 |
| 1530 | } |
tomhudson@google.com | 381010e | 2013-10-24 11:12:47 +0000 | [diff] [blame] | 1531 | } |
| 1532 | |
robertphillips@google.com | d550088 | 2014-04-02 23:51:13 +0000 | [diff] [blame] | 1533 | static void test_gen_id(skiatest::Reporter* reporter) { |
| 1534 | |
Robert Phillips | cfaeec4 | 2014-07-13 12:00:50 -0400 | [diff] [blame] | 1535 | SkPictureRecorder recorder; |
| 1536 | recorder.beginRecording(0, 0); |
| 1537 | SkAutoTUnref<SkPicture> empty(recorder.endRecording()); |
robertphillips@google.com | d550088 | 2014-04-02 23:51:13 +0000 | [diff] [blame] | 1538 | |
robertphillips@google.com | 84b18c7 | 2014-04-13 19:09:42 +0000 | [diff] [blame] | 1539 | // Empty pictures should still have a valid ID |
Robert Phillips | cfaeec4 | 2014-07-13 12:00:50 -0400 | [diff] [blame] | 1540 | REPORTER_ASSERT(reporter, empty->uniqueID() != SK_InvalidGenID); |
robertphillips@google.com | d550088 | 2014-04-02 23:51:13 +0000 | [diff] [blame] | 1541 | |
robertphillips | 9f1c241 | 2014-06-09 06:25:34 -0700 | [diff] [blame] | 1542 | SkCanvas* canvas = recorder.beginRecording(1, 1); |
robertphillips@google.com | 84b18c7 | 2014-04-13 19:09:42 +0000 | [diff] [blame] | 1543 | canvas->drawARGB(255, 255, 255, 255); |
| 1544 | SkAutoTUnref<SkPicture> hasData(recorder.endRecording()); |
| 1545 | // picture should have a non-zero id after recording |
| 1546 | REPORTER_ASSERT(reporter, hasData->uniqueID() != SK_InvalidGenID); |
robertphillips@google.com | d550088 | 2014-04-02 23:51:13 +0000 | [diff] [blame] | 1547 | |
robertphillips@google.com | 84b18c7 | 2014-04-13 19:09:42 +0000 | [diff] [blame] | 1548 | // both pictures should have different ids |
Robert Phillips | cfaeec4 | 2014-07-13 12:00:50 -0400 | [diff] [blame] | 1549 | REPORTER_ASSERT(reporter, hasData->uniqueID() != empty->uniqueID()); |
robertphillips@google.com | d550088 | 2014-04-02 23:51:13 +0000 | [diff] [blame] | 1550 | } |
| 1551 | |
tfarina@chromium.org | e4fafb1 | 2013-12-12 21:11:12 +0000 | [diff] [blame] | 1552 | DEF_TEST(Picture, reporter) { |
scroggo@google.com | d614c6a | 2012-09-14 17:26:37 +0000 | [diff] [blame] | 1553 | #ifdef SK_DEBUG |
robertphillips | db53990 | 2014-07-01 08:47:04 -0700 | [diff] [blame] | 1554 | test_deleting_empty_picture(); |
scroggo@google.com | d614c6a | 2012-09-14 17:26:37 +0000 | [diff] [blame] | 1555 | test_serializing_empty_picture(); |
scroggo@google.com | 4b90b11 | 2012-12-04 15:08:56 +0000 | [diff] [blame] | 1556 | #else |
| 1557 | test_bad_bitmap(); |
scroggo@google.com | d614c6a | 2012-09-14 17:26:37 +0000 | [diff] [blame] | 1558 | #endif |
commit-bot@chromium.org | ea7d08e | 2014-02-13 16:00:51 +0000 | [diff] [blame] | 1559 | test_unbalanced_save_restores(reporter); |
sugoi@google.com | 54f0d1b | 2013-02-27 19:17:41 +0000 | [diff] [blame] | 1560 | test_peephole(); |
robertphillips@google.com | b950c6f | 2014-04-25 00:02:12 +0000 | [diff] [blame] | 1561 | #if SK_SUPPORT_GPU |
commit-bot@chromium.org | e2cb12a | 2014-04-24 21:53:13 +0000 | [diff] [blame] | 1562 | test_gpu_veto(reporter); |
robertphillips@google.com | b950c6f | 2014-04-25 00:02:12 +0000 | [diff] [blame] | 1563 | #endif |
reed@google.com | fe7b1ed | 2012-11-29 21:00:39 +0000 | [diff] [blame] | 1564 | test_gatherpixelrefs(reporter); |
robertphillips@google.com | 56bf6e4 | 2014-01-13 13:33:26 +0000 | [diff] [blame] | 1565 | test_gatherpixelrefsandrects(reporter); |
scroggo@google.com | 7c9d539 | 2012-12-10 15:40:55 +0000 | [diff] [blame] | 1566 | test_bitmap_with_encoded_data(reporter); |
commit-bot@chromium.org | 70512af | 2014-03-18 17:45:32 +0000 | [diff] [blame] | 1567 | test_draw_empty(reporter); |
junov@chromium.org | d575eed | 2013-05-08 15:39:13 +0000 | [diff] [blame] | 1568 | test_clip_bound_opt(reporter); |
fmalita@google.com | d0f1a4f | 2013-08-27 15:50:19 +0000 | [diff] [blame] | 1569 | test_clip_expansion(reporter); |
tomhudson@google.com | 381010e | 2013-10-24 11:12:47 +0000 | [diff] [blame] | 1570 | test_hierarchical(reporter); |
robertphillips@google.com | d550088 | 2014-04-02 23:51:13 +0000 | [diff] [blame] | 1571 | test_gen_id(reporter); |
scroggo@google.com | d614c6a | 2012-09-14 17:26:37 +0000 | [diff] [blame] | 1572 | } |
commit-bot@chromium.org | 50b393a | 2014-02-10 18:29:10 +0000 | [diff] [blame] | 1573 | |
commit-bot@chromium.org | 0205aba | 2014-05-06 12:02:22 +0000 | [diff] [blame] | 1574 | #if SK_SUPPORT_GPU |
| 1575 | DEF_GPUTEST(GPUPicture, reporter, factory) { |
| 1576 | test_gpu_picture_optimization(reporter, factory); |
| 1577 | } |
| 1578 | #endif |
| 1579 | |
commit-bot@chromium.org | 50b393a | 2014-02-10 18:29:10 +0000 | [diff] [blame] | 1580 | static void draw_bitmaps(const SkBitmap bitmap, SkCanvas* canvas) { |
| 1581 | const SkPaint paint; |
| 1582 | const SkRect rect = { 5.0f, 5.0f, 8.0f, 8.0f }; |
| 1583 | const SkIRect irect = { 2, 2, 3, 3 }; |
| 1584 | |
| 1585 | // Don't care what these record, as long as they're legal. |
| 1586 | canvas->drawBitmap(bitmap, 0.0f, 0.0f, &paint); |
| 1587 | canvas->drawBitmapRectToRect(bitmap, &rect, rect, &paint, SkCanvas::kNone_DrawBitmapRectFlag); |
| 1588 | canvas->drawBitmapMatrix(bitmap, SkMatrix::I(), &paint); |
| 1589 | canvas->drawBitmapNine(bitmap, irect, rect, &paint); |
| 1590 | canvas->drawSprite(bitmap, 1, 1); |
| 1591 | } |
| 1592 | |
| 1593 | static void test_draw_bitmaps(SkCanvas* canvas) { |
| 1594 | SkBitmap empty; |
| 1595 | draw_bitmaps(empty, canvas); |
commit-bot@chromium.org | a3264e5 | 2014-05-30 13:26:10 +0000 | [diff] [blame] | 1596 | empty.setInfo(SkImageInfo::MakeN32Premul(10, 10)); |
commit-bot@chromium.org | 50b393a | 2014-02-10 18:29:10 +0000 | [diff] [blame] | 1597 | draw_bitmaps(empty, canvas); |
| 1598 | } |
| 1599 | |
| 1600 | DEF_TEST(Picture_EmptyBitmap, r) { |
robertphillips@google.com | 84b18c7 | 2014-04-13 19:09:42 +0000 | [diff] [blame] | 1601 | SkPictureRecorder recorder; |
robertphillips | 9f1c241 | 2014-06-09 06:25:34 -0700 | [diff] [blame] | 1602 | test_draw_bitmaps(recorder.beginRecording(10, 10)); |
robertphillips@google.com | 84b18c7 | 2014-04-13 19:09:42 +0000 | [diff] [blame] | 1603 | SkAutoTUnref<SkPicture> picture(recorder.endRecording()); |
commit-bot@chromium.org | 50b393a | 2014-02-10 18:29:10 +0000 | [diff] [blame] | 1604 | } |
| 1605 | |
| 1606 | DEF_TEST(Canvas_EmptyBitmap, r) { |
| 1607 | SkBitmap dst; |
mike@reedtribe.org | deee496 | 2014-02-13 14:41:43 +0000 | [diff] [blame] | 1608 | dst.allocN32Pixels(10, 10); |
commit-bot@chromium.org | 50b393a | 2014-02-10 18:29:10 +0000 | [diff] [blame] | 1609 | SkCanvas canvas(dst); |
| 1610 | |
| 1611 | test_draw_bitmaps(&canvas); |
| 1612 | } |
dneto | 3f22e8c | 2014-07-30 15:42:22 -0700 | [diff] [blame] | 1613 | |
| 1614 | DEF_TEST(DontOptimizeSaveLayerDrawDrawRestore, reporter) { |
| 1615 | // This test is from crbug.com/344987. |
| 1616 | // The commands are: |
| 1617 | // saveLayer with paint that modifies alpha |
| 1618 | // drawBitmapRectToRect |
| 1619 | // drawBitmapRectToRect |
| 1620 | // restore |
| 1621 | // The bug was that this structure was modified so that: |
| 1622 | // - The saveLayer and restore were eliminated |
| 1623 | // - The alpha was only applied to the first drawBitmapRectToRect |
| 1624 | |
| 1625 | // This test draws blue and red squares inside a 50% transparent |
| 1626 | // layer. Both colours should show up muted. |
| 1627 | // When the bug is present, the red square (the second bitmap) |
| 1628 | // shows upwith full opacity. |
| 1629 | |
| 1630 | SkBitmap blueBM; |
| 1631 | make_bm(&blueBM, 100, 100, SkColorSetARGB(255, 0, 0, 255), true); |
| 1632 | SkBitmap redBM; |
| 1633 | make_bm(&redBM, 100, 100, SkColorSetARGB(255, 255, 0, 0), true); |
| 1634 | SkPaint semiTransparent; |
| 1635 | semiTransparent.setAlpha(0x80); |
| 1636 | |
| 1637 | SkPictureRecorder recorder; |
| 1638 | SkCanvas* canvas = recorder.beginRecording(100, 100); |
| 1639 | canvas->drawARGB(0, 0, 0, 0); |
| 1640 | |
| 1641 | canvas->saveLayer(0, &semiTransparent); |
| 1642 | canvas->drawBitmap(blueBM, 25, 25); |
| 1643 | canvas->drawBitmap(redBM, 50, 50); |
| 1644 | canvas->restore(); |
| 1645 | |
| 1646 | SkAutoTUnref<SkPicture> picture(recorder.endRecording()); |
| 1647 | |
| 1648 | // Now replay the picture back on another canvas |
| 1649 | // and check a couple of its pixels. |
| 1650 | SkBitmap replayBM; |
| 1651 | make_bm(&replayBM, 100, 100, SK_ColorBLACK, false); |
| 1652 | SkCanvas replayCanvas(replayBM); |
| 1653 | picture->draw(&replayCanvas); |
| 1654 | replayCanvas.flush(); |
| 1655 | |
| 1656 | // With the bug present, at (55, 55) we would get a fully opaque red |
| 1657 | // intead of a dark red. |
| 1658 | REPORTER_ASSERT(reporter, replayBM.getColor(30, 30) == 0xff000080); |
| 1659 | REPORTER_ASSERT(reporter, replayBM.getColor(55, 55) == 0xff800000); |
| 1660 | } |