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