| 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 | 60c2a79 | 2014-08-18 15:07:13 -0700 | [diff] [blame] | 581 | #define GENERATE_CANVAS(recorder, x) \ |
| 582 | (x) ? recorder.EXPERIMENTAL_beginRecording(100, 100) \ |
| 583 | : recorder.beginRecording(100,100); |
| 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 | #undef GENERATE_CANVAS |
| 618 | |
| robertphillips@google.com | 56bf6e4 | 2014-01-13 13:33:26 +0000 | [diff] [blame] | 619 | static void test_gatherpixelrefsandrects(skiatest::Reporter* reporter) { |
| 620 | const int IW = 32; |
| 621 | const int IH = IW; |
| 622 | const SkScalar W = SkIntToScalar(IW); |
| 623 | const SkScalar H = W; |
| 624 | |
| 625 | static const int N = 4; |
| 626 | SkBitmap bm[2*N]; |
| 627 | SkPixelRef* refs[2*N]; |
| 628 | SkTDArray<SkPixelRef*> analytic[N]; |
| 629 | |
| 630 | const SkPoint pos[N] = { |
| 631 | { 0, 0 }, { W, 0 }, { 0, H }, { W, H } |
| 632 | }; |
| 633 | |
| 634 | create_textures(bm, refs, N, IW, IH); |
| 635 | |
| 636 | SkRandom rand; |
| 637 | 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] | 638 | SkAutoTUnref<SkPicture> pic( |
| 639 | record_bitmaps(bm, pos, analytic, N, gProcs[k].proc)); |
| robertphillips@google.com | 56bf6e4 | 2014-01-13 13:33:26 +0000 | [diff] [blame] | 640 | |
| 641 | REPORTER_ASSERT(reporter, pic->willPlayBackBitmaps() || N == 0); |
| 642 | |
| 643 | SkAutoTUnref<SkPictureUtils::SkPixelRefContainer> prCont( |
| 644 | new SkPictureUtils::SkPixelRefsAndRectsList); |
| 645 | |
| 646 | SkPictureUtils::GatherPixelRefsAndRects(pic, prCont); |
| 647 | |
| 648 | // quick check for a small piece of each quadrant, which should just |
| 649 | // contain 1 or 2 bitmaps. |
| 650 | for (size_t i = 0; i < SK_ARRAY_COUNT(pos); ++i) { |
| 651 | SkRect r; |
| 652 | r.set(2, 2, W - 2, H - 2); |
| 653 | r.offset(pos[i].fX, pos[i].fY); |
| 654 | |
| 655 | SkTDArray<SkPixelRef*> gatheredRefs; |
| 656 | prCont->query(r, &gatheredRefs); |
| 657 | |
| 658 | int count = gatheredRefs.count(); |
| 659 | REPORTER_ASSERT(reporter, 1 == count || 2 == count); |
| 660 | if (1 == count) { |
| 661 | REPORTER_ASSERT(reporter, gatheredRefs[0] == refs[i]); |
| 662 | } else if (2 == count) { |
| skia.committer@gmail.com | 2e9a715 | 2014-01-14 07:01:42 +0000 | [diff] [blame] | 663 | REPORTER_ASSERT(reporter, |
| robertphillips@google.com | 56bf6e4 | 2014-01-13 13:33:26 +0000 | [diff] [blame] | 664 | (gatheredRefs[0] == refs[i] && gatheredRefs[1] == refs[i+N]) || |
| 665 | (gatheredRefs[1] == refs[i] && gatheredRefs[0] == refs[i+N])); |
| 666 | } |
| 667 | } |
| 668 | |
| 669 | SkBitmap image; |
| 670 | draw(pic, 2*IW, 2*IH, &image); |
| 671 | |
| 672 | // Test a bunch of random (mostly) rects, and compare the gather results |
| 673 | // with the analytic results and the pixel refs seen in a rendering. |
| 674 | for (int j = 0; j < 100; ++j) { |
| 675 | SkRect r; |
| 676 | rand_rect(&r, rand, 2*W, 2*H); |
| 677 | |
| 678 | SkTDArray<SkPixelRef*> fromImage; |
| 679 | gather_from_image(image, refs, N, &fromImage, r); |
| 680 | |
| 681 | SkTDArray<SkPixelRef*> fromAnalytic; |
| 682 | gather_from_analytic(pos, W, H, analytic, N, &fromAnalytic, r); |
| 683 | |
| 684 | SkTDArray<SkPixelRef*> gatheredRefs; |
| 685 | prCont->query(r, &gatheredRefs); |
| 686 | |
| 687 | // Everything that we saw drawn should appear in the analytic list |
| 688 | // but the analytic list may contain some pixelRefs that were not |
| 689 | // seen in the image (e.g., A8 textures used as masks) |
| 690 | for (int i = 0; i < fromImage.count(); ++i) { |
| 691 | REPORTER_ASSERT(reporter, -1 != fromAnalytic.find(fromImage[i])); |
| 692 | } |
| 693 | |
| 694 | // Everything in the analytic list should appear in the gathered |
| skia.committer@gmail.com | 2e9a715 | 2014-01-14 07:01:42 +0000 | [diff] [blame] | 695 | // list. |
| robertphillips@google.com | 56bf6e4 | 2014-01-13 13:33:26 +0000 | [diff] [blame] | 696 | for (int i = 0; i < fromAnalytic.count(); ++i) { |
| 697 | REPORTER_ASSERT(reporter, -1 != gatheredRefs.find(fromAnalytic[i])); |
| 698 | } |
| 699 | } |
| 700 | } |
| 701 | } |
| 702 | |
| scroggo@google.com | d614c6a | 2012-09-14 17:26:37 +0000 | [diff] [blame] | 703 | #ifdef SK_DEBUG |
| mtklein | 3e8232b | 2014-08-18 13:39:11 -0700 | [diff] [blame] | 704 | // Ensure that deleting an empty SkPicture does not assert. Asserts only fire |
| robertphillips | db53990 | 2014-07-01 08:47:04 -0700 | [diff] [blame] | 705 | // in debug mode, so only run in debug mode. |
| 706 | static void test_deleting_empty_picture() { |
| robertphillips@google.com | 84b18c7 | 2014-04-13 19:09:42 +0000 | [diff] [blame] | 707 | SkPictureRecorder recorder; |
| scroggo@google.com | d614c6a | 2012-09-14 17:26:37 +0000 | [diff] [blame] | 708 | // Creates an SkPictureRecord |
| robertphillips | 9f1c241 | 2014-06-09 06:25:34 -0700 | [diff] [blame] | 709 | recorder.beginRecording(0, 0); |
| robertphillips | db53990 | 2014-07-01 08:47:04 -0700 | [diff] [blame] | 710 | // Turns that into an SkPicture |
| robertphillips@google.com | 84b18c7 | 2014-04-13 19:09:42 +0000 | [diff] [blame] | 711 | SkAutoTUnref<SkPicture> picture(recorder.endRecording()); |
| robertphillips | db53990 | 2014-07-01 08:47:04 -0700 | [diff] [blame] | 712 | // Ceates a new SkPictureRecord |
| robertphillips | 9f1c241 | 2014-06-09 06:25:34 -0700 | [diff] [blame] | 713 | recorder.beginRecording(0, 0); |
| scroggo@google.com | d614c6a | 2012-09-14 17:26:37 +0000 | [diff] [blame] | 714 | } |
| 715 | |
| 716 | // Ensure that serializing an empty picture does not assert. Likewise only runs in debug mode. |
| 717 | static void test_serializing_empty_picture() { |
| robertphillips@google.com | 84b18c7 | 2014-04-13 19:09:42 +0000 | [diff] [blame] | 718 | SkPictureRecorder recorder; |
| robertphillips | 9f1c241 | 2014-06-09 06:25:34 -0700 | [diff] [blame] | 719 | recorder.beginRecording(0, 0); |
| robertphillips@google.com | 84b18c7 | 2014-04-13 19:09:42 +0000 | [diff] [blame] | 720 | SkAutoTUnref<SkPicture> picture(recorder.endRecording()); |
| scroggo@google.com | d614c6a | 2012-09-14 17:26:37 +0000 | [diff] [blame] | 721 | SkDynamicMemoryWStream stream; |
| robertphillips@google.com | 84b18c7 | 2014-04-13 19:09:42 +0000 | [diff] [blame] | 722 | picture->serialize(&stream); |
| scroggo@google.com | d614c6a | 2012-09-14 17:26:37 +0000 | [diff] [blame] | 723 | } |
| 724 | #endif |
| 725 | |
| commit-bot@chromium.org | e0e7cfe | 2013-09-09 20:09:12 +0000 | [diff] [blame] | 726 | static void rand_op(SkCanvas* canvas, SkRandom& rand) { |
| reed@google.com | 21b519d | 2012-10-02 17:42:15 +0000 | [diff] [blame] | 727 | SkPaint paint; |
| 728 | SkRect rect = SkRect::MakeWH(50, 50); |
| 729 | |
| 730 | SkScalar unit = rand.nextUScalar1(); |
| 731 | if (unit <= 0.3) { |
| 732 | // SkDebugf("save\n"); |
| 733 | canvas->save(); |
| 734 | } else if (unit <= 0.6) { |
| 735 | // SkDebugf("restore\n"); |
| 736 | canvas->restore(); |
| 737 | } else if (unit <= 0.9) { |
| 738 | // SkDebugf("clip\n"); |
| 739 | canvas->clipRect(rect); |
| 740 | } else { |
| 741 | // SkDebugf("draw\n"); |
| 742 | canvas->drawPaint(paint); |
| 743 | } |
| 744 | } |
| 745 | |
| robertphillips@google.com | b950c6f | 2014-04-25 00:02:12 +0000 | [diff] [blame] | 746 | #if SK_SUPPORT_GPU |
| tomhudson | 60c2a79 | 2014-08-18 15:07:13 -0700 | [diff] [blame] | 747 | #define GENERATE_CANVAS(recorder, x) \ |
| 748 | (x) ? recorder.EXPERIMENTAL_beginRecording(100, 100) \ |
| 749 | : recorder.beginRecording(100,100); |
| 750 | |
| 751 | static void test_gpu_veto(skiatest::Reporter* reporter, |
| 752 | bool useNewPath) { |
| commit-bot@chromium.org | e2cb12a | 2014-04-24 21:53:13 +0000 | [diff] [blame] | 753 | |
| 754 | SkPictureRecorder recorder; |
| 755 | |
| tomhudson | 60c2a79 | 2014-08-18 15:07:13 -0700 | [diff] [blame] | 756 | SkCanvas* canvas = GENERATE_CANVAS(recorder, useNewPath); |
| commit-bot@chromium.org | e2cb12a | 2014-04-24 21:53:13 +0000 | [diff] [blame] | 757 | { |
| 758 | SkPath path; |
| 759 | path.moveTo(0, 0); |
| 760 | path.lineTo(50, 50); |
| 761 | |
| 762 | SkScalar intervals[] = { 1.0f, 1.0f }; |
| 763 | SkAutoTUnref<SkDashPathEffect> dash(SkDashPathEffect::Create(intervals, 2, 0)); |
| 764 | |
| 765 | SkPaint paint; |
| 766 | paint.setStyle(SkPaint::kStroke_Style); |
| 767 | paint.setPathEffect(dash); |
| 768 | |
| 769 | canvas->drawPath(path, paint); |
| 770 | } |
| 771 | SkAutoTUnref<SkPicture> picture(recorder.endRecording()); |
| 772 | // path effects currently render an SkPicture undesireable for GPU rendering |
| commit-bot@chromium.org | a1ff26a | 2014-05-30 21:52:52 +0000 | [diff] [blame] | 773 | |
| 774 | const char *reason = NULL; |
| 775 | REPORTER_ASSERT(reporter, !picture->suitableForGpuRasterization(NULL, &reason)); |
| 776 | REPORTER_ASSERT(reporter, NULL != reason); |
| commit-bot@chromium.org | e2cb12a | 2014-04-24 21:53:13 +0000 | [diff] [blame] | 777 | |
| tomhudson | 60c2a79 | 2014-08-18 15:07:13 -0700 | [diff] [blame] | 778 | canvas = GENERATE_CANVAS(recorder, useNewPath); |
| commit-bot@chromium.org | e2cb12a | 2014-04-24 21:53:13 +0000 | [diff] [blame] | 779 | { |
| 780 | SkPath path; |
| 781 | |
| 782 | path.moveTo(0, 0); |
| 783 | path.lineTo(0, 50); |
| 784 | path.lineTo(25, 25); |
| 785 | path.lineTo(50, 50); |
| 786 | path.lineTo(50, 0); |
| 787 | path.close(); |
| 788 | REPORTER_ASSERT(reporter, !path.isConvex()); |
| 789 | |
| 790 | SkPaint paint; |
| 791 | paint.setAntiAlias(true); |
| 792 | for (int i = 0; i < 50; ++i) { |
| 793 | canvas->drawPath(path, paint); |
| 794 | } |
| 795 | } |
| 796 | picture.reset(recorder.endRecording()); |
| 797 | // A lot of AA concave paths currently render an SkPicture undesireable for GPU rendering |
| 798 | REPORTER_ASSERT(reporter, !picture->suitableForGpuRasterization(NULL)); |
| 799 | |
| tomhudson | 60c2a79 | 2014-08-18 15:07:13 -0700 | [diff] [blame] | 800 | canvas = GENERATE_CANVAS(recorder, useNewPath); |
| commit-bot@chromium.org | e2cb12a | 2014-04-24 21:53:13 +0000 | [diff] [blame] | 801 | { |
| 802 | SkPath path; |
| 803 | |
| 804 | path.moveTo(0, 0); |
| 805 | path.lineTo(0, 50); |
| 806 | path.lineTo(25, 25); |
| 807 | path.lineTo(50, 50); |
| 808 | path.lineTo(50, 0); |
| 809 | path.close(); |
| 810 | REPORTER_ASSERT(reporter, !path.isConvex()); |
| 811 | |
| 812 | SkPaint paint; |
| 813 | paint.setAntiAlias(true); |
| 814 | paint.setStyle(SkPaint::kStroke_Style); |
| 815 | paint.setStrokeWidth(0); |
| 816 | for (int i = 0; i < 50; ++i) { |
| 817 | canvas->drawPath(path, paint); |
| 818 | } |
| 819 | } |
| 820 | picture.reset(recorder.endRecording()); |
| 821 | // hairline stroked AA concave paths are fine for GPU rendering |
| 822 | REPORTER_ASSERT(reporter, picture->suitableForGpuRasterization(NULL)); |
| tomhudson | 60c2a79 | 2014-08-18 15:07:13 -0700 | [diff] [blame] | 823 | |
| 824 | canvas = GENERATE_CANVAS(recorder, useNewPath); |
| 825 | { |
| 826 | SkPaint paint; |
| 827 | SkScalar intervals [] = { 10, 20 }; |
| 828 | SkPathEffect* pe = SkDashPathEffect::Create(intervals, 2, 25); |
| 829 | paint.setPathEffect(pe)->unref(); |
| 830 | |
| 831 | SkPoint points [2] = { { 0, 0 }, { 100, 0 } }; |
| 832 | canvas->drawPoints(SkCanvas::kLines_PointMode, 2, points, paint); |
| 833 | } |
| 834 | picture.reset(recorder.endRecording()); |
| 835 | // fast-path dashed effects are fine for GPU rendering ... |
| 836 | REPORTER_ASSERT(reporter, picture->suitableForGpuRasterization(NULL)); |
| 837 | |
| 838 | canvas = GENERATE_CANVAS(recorder, useNewPath); |
| 839 | { |
| 840 | SkPaint paint; |
| 841 | SkScalar intervals [] = { 10, 20 }; |
| 842 | SkPathEffect* pe = SkDashPathEffect::Create(intervals, 2, 25); |
| 843 | paint.setPathEffect(pe)->unref(); |
| 844 | |
| 845 | canvas->drawRect(SkRect::MakeWH(10, 10), paint); |
| 846 | } |
| 847 | picture.reset(recorder.endRecording()); |
| 848 | // ... but only when applied to drawPoint() calls |
| 849 | REPORTER_ASSERT(reporter, !picture->suitableForGpuRasterization(NULL)); |
| commit-bot@chromium.org | e2cb12a | 2014-04-24 21:53:13 +0000 | [diff] [blame] | 850 | } |
| commit-bot@chromium.org | 0205aba | 2014-05-06 12:02:22 +0000 | [diff] [blame] | 851 | |
| tomhudson | 60c2a79 | 2014-08-18 15:07:13 -0700 | [diff] [blame] | 852 | #undef GENERATE_CANVAS |
| 853 | |
| commit-bot@chromium.org | 0205aba | 2014-05-06 12:02:22 +0000 | [diff] [blame] | 854 | static void test_gpu_picture_optimization(skiatest::Reporter* reporter, |
| 855 | GrContextFactory* factory) { |
| bsalomon | e904c09 | 2014-07-17 10:50:59 -0700 | [diff] [blame] | 856 | for (int i= 0; i < GrContextFactory::kGLContextTypeCnt; ++i) { |
| 857 | GrContextFactory::GLContextType glCtxType = (GrContextFactory::GLContextType) i; |
| commit-bot@chromium.org | 0205aba | 2014-05-06 12:02:22 +0000 | [diff] [blame] | 858 | |
| bsalomon | e904c09 | 2014-07-17 10:50:59 -0700 | [diff] [blame] | 859 | if (!GrContextFactory::IsRenderingGLContext(glCtxType)) { |
| 860 | continue; |
| 861 | } |
| commit-bot@chromium.org | 0205aba | 2014-05-06 12:02:22 +0000 | [diff] [blame] | 862 | |
| bsalomon | e904c09 | 2014-07-17 10:50:59 -0700 | [diff] [blame] | 863 | GrContext* context = factory->get(glCtxType); |
| commit-bot@chromium.org | 0205aba | 2014-05-06 12:02:22 +0000 | [diff] [blame] | 864 | |
| bsalomon | e904c09 | 2014-07-17 10:50:59 -0700 | [diff] [blame] | 865 | if (NULL == context) { |
| 866 | continue; |
| 867 | } |
| commit-bot@chromium.org | 0205aba | 2014-05-06 12:02:22 +0000 | [diff] [blame] | 868 | |
| bsalomon | e904c09 | 2014-07-17 10:50:59 -0700 | [diff] [blame] | 869 | static const int kWidth = 100; |
| 870 | static const int kHeight = 100; |
| 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 | SkAutoTUnref<SkPicture> pict; |
| 873 | |
| 874 | // create a picture with the structure: |
| commit-bot@chromium.org | 0205aba | 2014-05-06 12:02:22 +0000 | [diff] [blame] | 875 | // 1) |
| bsalomon | e904c09 | 2014-07-17 10:50:59 -0700 | [diff] [blame] | 876 | // SaveLayer |
| 877 | // Restore |
| commit-bot@chromium.org | 0205aba | 2014-05-06 12:02:22 +0000 | [diff] [blame] | 878 | // 2) |
| bsalomon | e904c09 | 2014-07-17 10:50:59 -0700 | [diff] [blame] | 879 | // SaveLayer |
| 880 | // Translate |
| 881 | // SaveLayer w/ bound |
| 882 | // Restore |
| 883 | // Restore |
| commit-bot@chromium.org | 0205aba | 2014-05-06 12:02:22 +0000 | [diff] [blame] | 884 | // 3) |
| bsalomon | e904c09 | 2014-07-17 10:50:59 -0700 | [diff] [blame] | 885 | // SaveLayer w/ copyable paint |
| 886 | // Restore |
| commit-bot@chromium.org | 0205aba | 2014-05-06 12:02:22 +0000 | [diff] [blame] | 887 | // 4) |
| bsalomon | e904c09 | 2014-07-17 10:50:59 -0700 | [diff] [blame] | 888 | // SaveLayer w/ non-copyable paint |
| 889 | // Restore |
| commit-bot@chromium.org | 0205aba | 2014-05-06 12:02:22 +0000 | [diff] [blame] | 890 | { |
| bsalomon | e904c09 | 2014-07-17 10:50:59 -0700 | [diff] [blame] | 891 | SkPictureRecorder recorder; |
| 892 | |
| 893 | SkCanvas* c = recorder.beginRecording(kWidth, kHeight); |
| 894 | // 1) |
| 895 | c->saveLayer(NULL, NULL); |
| commit-bot@chromium.org | 0205aba | 2014-05-06 12:02:22 +0000 | [diff] [blame] | 896 | c->restore(); |
| bsalomon | e904c09 | 2014-07-17 10:50:59 -0700 | [diff] [blame] | 897 | |
| 898 | // 2) |
| 899 | c->saveLayer(NULL, NULL); |
| 900 | c->translate(kWidth/2, kHeight/2); |
| 901 | SkRect r = SkRect::MakeXYWH(0, 0, kWidth/2, kHeight/2); |
| 902 | c->saveLayer(&r, NULL); |
| 903 | c->restore(); |
| 904 | c->restore(); |
| 905 | |
| 906 | // 3) |
| 907 | { |
| 908 | SkPaint p; |
| 909 | p.setColor(SK_ColorRED); |
| 910 | c->saveLayer(NULL, &p); |
| 911 | c->restore(); |
| 912 | } |
| 913 | // 4) |
| 914 | // TODO: this case will need to be removed once the paint's are immutable |
| 915 | { |
| 916 | SkPaint p; |
| 917 | SkAutoTUnref<SkColorFilter> cf(SkLumaColorFilter::Create()); |
| 918 | p.setImageFilter(SkColorFilterImageFilter::Create(cf.get()))->unref(); |
| 919 | c->saveLayer(NULL, &p); |
| 920 | c->restore(); |
| 921 | } |
| 922 | |
| 923 | pict.reset(recorder.endRecording()); |
| commit-bot@chromium.org | 0205aba | 2014-05-06 12:02:22 +0000 | [diff] [blame] | 924 | } |
| 925 | |
| bsalomon | e904c09 | 2014-07-17 10:50:59 -0700 | [diff] [blame] | 926 | // Now test out the SaveLayer extraction |
| 927 | { |
| 928 | SkImageInfo info = SkImageInfo::MakeN32Premul(kWidth, kHeight); |
| commit-bot@chromium.org | 0205aba | 2014-05-06 12:02:22 +0000 | [diff] [blame] | 929 | |
| bsalomon | e904c09 | 2014-07-17 10:50:59 -0700 | [diff] [blame] | 930 | SkAutoTUnref<SkSurface> surface(SkSurface::NewScratchRenderTarget(context, info)); |
| commit-bot@chromium.org | 0205aba | 2014-05-06 12:02:22 +0000 | [diff] [blame] | 931 | |
| bsalomon | e904c09 | 2014-07-17 10:50:59 -0700 | [diff] [blame] | 932 | SkCanvas* canvas = surface->getCanvas(); |
| commit-bot@chromium.org | 0205aba | 2014-05-06 12:02:22 +0000 | [diff] [blame] | 933 | |
| bsalomon | e904c09 | 2014-07-17 10:50:59 -0700 | [diff] [blame] | 934 | canvas->EXPERIMENTAL_optimize(pict); |
| commit-bot@chromium.org | 0205aba | 2014-05-06 12:02:22 +0000 | [diff] [blame] | 935 | |
| robertphillips | 6617d50 | 2014-08-18 09:39:34 -0700 | [diff] [blame] | 936 | SkPicture::AccelData::Key key = GrAccelData::ComputeAccelDataKey(); |
| commit-bot@chromium.org | 0205aba | 2014-05-06 12:02:22 +0000 | [diff] [blame] | 937 | |
| bsalomon | e904c09 | 2014-07-17 10:50:59 -0700 | [diff] [blame] | 938 | const SkPicture::AccelData* data = pict->EXPERIMENTAL_getAccelData(key); |
| 939 | REPORTER_ASSERT(reporter, NULL != data); |
| commit-bot@chromium.org | 0205aba | 2014-05-06 12:02:22 +0000 | [diff] [blame] | 940 | |
| robertphillips | 6617d50 | 2014-08-18 09:39:34 -0700 | [diff] [blame] | 941 | const GrAccelData *gpuData = static_cast<const GrAccelData*>(data); |
| bsalomon | e904c09 | 2014-07-17 10:50:59 -0700 | [diff] [blame] | 942 | REPORTER_ASSERT(reporter, 5 == gpuData->numSaveLayers()); |
| commit-bot@chromium.org | 0205aba | 2014-05-06 12:02:22 +0000 | [diff] [blame] | 943 | |
| robertphillips | 6617d50 | 2014-08-18 09:39:34 -0700 | [diff] [blame] | 944 | const GrAccelData::SaveLayerInfo& info0 = gpuData->saveLayerInfo(0); |
| bsalomon | e904c09 | 2014-07-17 10:50:59 -0700 | [diff] [blame] | 945 | // The parent/child layer appear in reverse order |
| robertphillips | 6617d50 | 2014-08-18 09:39:34 -0700 | [diff] [blame] | 946 | const GrAccelData::SaveLayerInfo& info1 = gpuData->saveLayerInfo(2); |
| 947 | const GrAccelData::SaveLayerInfo& info2 = gpuData->saveLayerInfo(1); |
| 948 | const GrAccelData::SaveLayerInfo& info3 = gpuData->saveLayerInfo(3); |
| 949 | // const GrAccelData::SaveLayerInfo& info4 = gpuData->saveLayerInfo(4); |
| commit-bot@chromium.org | 0205aba | 2014-05-06 12:02:22 +0000 | [diff] [blame] | 950 | |
| bsalomon | e904c09 | 2014-07-17 10:50:59 -0700 | [diff] [blame] | 951 | REPORTER_ASSERT(reporter, info0.fValid); |
| 952 | REPORTER_ASSERT(reporter, kWidth == info0.fSize.fWidth && kHeight == info0.fSize.fHeight); |
| 953 | REPORTER_ASSERT(reporter, info0.fCTM.isIdentity()); |
| 954 | REPORTER_ASSERT(reporter, 0 == info0.fOffset.fX && 0 == info0.fOffset.fY); |
| 955 | REPORTER_ASSERT(reporter, NULL != info0.fPaint); |
| 956 | REPORTER_ASSERT(reporter, !info0.fIsNested && !info0.fHasNestedLayers); |
| 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, info1.fValid); |
| 959 | REPORTER_ASSERT(reporter, kWidth == info1.fSize.fWidth && kHeight == info1.fSize.fHeight); |
| 960 | REPORTER_ASSERT(reporter, info1.fCTM.isIdentity()); |
| 961 | REPORTER_ASSERT(reporter, 0 == info1.fOffset.fX && 0 == info1.fOffset.fY); |
| 962 | REPORTER_ASSERT(reporter, NULL != info1.fPaint); |
| 963 | 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] | 964 | |
| bsalomon | e904c09 | 2014-07-17 10:50:59 -0700 | [diff] [blame] | 965 | REPORTER_ASSERT(reporter, info2.fValid); |
| 966 | REPORTER_ASSERT(reporter, kWidth/2 == info2.fSize.fWidth && |
| 967 | kHeight/2 == info2.fSize.fHeight); // bound reduces size |
| 968 | REPORTER_ASSERT(reporter, info2.fCTM.isIdentity()); // translated |
| 969 | REPORTER_ASSERT(reporter, kWidth/2 == info2.fOffset.fX && |
| 970 | kHeight/2 == info2.fOffset.fY); |
| 971 | REPORTER_ASSERT(reporter, NULL != info1.fPaint); |
| 972 | REPORTER_ASSERT(reporter, info2.fIsNested && !info2.fHasNestedLayers); // is nested |
| commit-bot@chromium.org | 0205aba | 2014-05-06 12:02:22 +0000 | [diff] [blame] | 973 | |
| bsalomon | e904c09 | 2014-07-17 10:50:59 -0700 | [diff] [blame] | 974 | REPORTER_ASSERT(reporter, info3.fValid); |
| 975 | REPORTER_ASSERT(reporter, kWidth == info3.fSize.fWidth && kHeight == info3.fSize.fHeight); |
| 976 | REPORTER_ASSERT(reporter, info3.fCTM.isIdentity()); |
| 977 | REPORTER_ASSERT(reporter, 0 == info3.fOffset.fX && 0 == info3.fOffset.fY); |
| 978 | REPORTER_ASSERT(reporter, NULL != info3.fPaint); |
| 979 | REPORTER_ASSERT(reporter, !info3.fIsNested && !info3.fHasNestedLayers); |
| 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 | #if 0 // needs more though for GrGatherCanvas |
| 982 | REPORTER_ASSERT(reporter, !info4.fValid); // paint is/was uncopyable |
| 983 | REPORTER_ASSERT(reporter, kWidth == info4.fSize.fWidth && kHeight == info4.fSize.fHeight); |
| 984 | REPORTER_ASSERT(reporter, 0 == info4.fOffset.fX && 0 == info4.fOffset.fY); |
| 985 | REPORTER_ASSERT(reporter, info4.fCTM.isIdentity()); |
| 986 | REPORTER_ASSERT(reporter, NULL == info4.fPaint); // paint is/was uncopyable |
| 987 | REPORTER_ASSERT(reporter, !info4.fIsNested && !info4.fHasNestedLayers); |
| 988 | #endif |
| 989 | } |
| commit-bot@chromium.org | 0205aba | 2014-05-06 12:02:22 +0000 | [diff] [blame] | 990 | } |
| 991 | } |
| 992 | |
| robertphillips@google.com | b950c6f | 2014-04-25 00:02:12 +0000 | [diff] [blame] | 993 | #endif |
| commit-bot@chromium.org | e2cb12a | 2014-04-24 21:53:13 +0000 | [diff] [blame] | 994 | |
| ajuma | 750ae26 | 2014-08-18 12:59:55 -0700 | [diff] [blame] | 995 | static void test_has_text(skiatest::Reporter* reporter) { |
| 996 | SkPictureRecorder recorder; |
| 997 | SkPaint paint; |
| 998 | paint.setColor(SK_ColorBLUE); |
| 999 | SkPoint point = SkPoint::Make(10, 10); |
| 1000 | |
| 1001 | SkCanvas* canvas = recorder.beginRecording(100, 100); |
| 1002 | { |
| 1003 | canvas->drawRect(SkRect::MakeWH(20, 20), paint); |
| 1004 | } |
| 1005 | SkAutoTUnref<SkPicture> picture(recorder.endRecording()); |
| 1006 | REPORTER_ASSERT(reporter, !picture->hasText()); |
| 1007 | |
| 1008 | canvas = recorder.beginRecording(100, 100); |
| 1009 | { |
| 1010 | canvas->drawText("Q", 1, point.fX, point.fY, paint); |
| 1011 | } |
| 1012 | picture.reset(recorder.endRecording()); |
| 1013 | REPORTER_ASSERT(reporter, picture->hasText()); |
| 1014 | |
| 1015 | canvas = recorder.beginRecording(100, 100); |
| 1016 | { |
| 1017 | canvas->drawPosText("Q", 1, &point, paint); |
| 1018 | } |
| 1019 | picture.reset(recorder.endRecording()); |
| 1020 | REPORTER_ASSERT(reporter, picture->hasText()); |
| 1021 | |
| 1022 | canvas = recorder.beginRecording(100, 100); |
| 1023 | { |
| 1024 | canvas->drawPosTextH("Q", 1, &point.fX, point.fY, paint); |
| 1025 | } |
| 1026 | picture.reset(recorder.endRecording()); |
| 1027 | REPORTER_ASSERT(reporter, picture->hasText()); |
| 1028 | |
| 1029 | canvas = recorder.beginRecording(100, 100); |
| 1030 | { |
| 1031 | SkPath path; |
| 1032 | path.moveTo(0, 0); |
| 1033 | path.lineTo(50, 50); |
| 1034 | |
| 1035 | canvas->drawTextOnPathHV("Q", 1, path, point.fX, point.fY, paint); |
| 1036 | } |
| 1037 | picture.reset(recorder.endRecording()); |
| 1038 | REPORTER_ASSERT(reporter, picture->hasText()); |
| 1039 | |
| 1040 | canvas = recorder.beginRecording(100, 100); |
| 1041 | { |
| 1042 | SkPath path; |
| 1043 | path.moveTo(0, 0); |
| 1044 | path.lineTo(50, 50); |
| 1045 | |
| 1046 | canvas->drawTextOnPath("Q", 1, path, NULL, paint); |
| 1047 | } |
| 1048 | picture.reset(recorder.endRecording()); |
| 1049 | REPORTER_ASSERT(reporter, picture->hasText()); |
| 1050 | } |
| 1051 | |
| commit-bot@chromium.org | ea7d08e | 2014-02-13 16:00:51 +0000 | [diff] [blame] | 1052 | static void set_canvas_to_save_count_4(SkCanvas* canvas) { |
| 1053 | canvas->restoreToCount(1); |
| 1054 | canvas->save(); |
| 1055 | canvas->save(); |
| 1056 | canvas->save(); |
| 1057 | } |
| 1058 | |
| commit-bot@chromium.org | 6d3eaea | 2014-05-27 23:41:45 +0000 | [diff] [blame] | 1059 | /** |
| 1060 | * A canvas that records the number of saves, saveLayers and restores. |
| 1061 | */ |
| 1062 | class SaveCountingCanvas : public SkCanvas { |
| 1063 | public: |
| 1064 | SaveCountingCanvas(int width, int height) |
| 1065 | : INHERITED(width, height) |
| 1066 | , fSaveCount(0) |
| 1067 | , fSaveLayerCount(0) |
| 1068 | , fRestoreCount(0){ |
| 1069 | } |
| 1070 | |
| 1071 | virtual SaveLayerStrategy willSaveLayer(const SkRect* bounds, const SkPaint* paint, |
| 1072 | SaveFlags flags) SK_OVERRIDE { |
| 1073 | ++fSaveLayerCount; |
| 1074 | return this->INHERITED::willSaveLayer(bounds, paint, flags); |
| 1075 | } |
| 1076 | |
| Florin Malita | 5f6102d | 2014-06-30 10:13:28 -0400 | [diff] [blame] | 1077 | virtual void willSave() SK_OVERRIDE { |
| commit-bot@chromium.org | 6d3eaea | 2014-05-27 23:41:45 +0000 | [diff] [blame] | 1078 | ++fSaveCount; |
| Florin Malita | 5f6102d | 2014-06-30 10:13:28 -0400 | [diff] [blame] | 1079 | this->INHERITED::willSave(); |
| commit-bot@chromium.org | 6d3eaea | 2014-05-27 23:41:45 +0000 | [diff] [blame] | 1080 | } |
| 1081 | |
| 1082 | virtual void willRestore() SK_OVERRIDE { |
| 1083 | ++fRestoreCount; |
| 1084 | this->INHERITED::willRestore(); |
| 1085 | } |
| 1086 | |
| 1087 | unsigned int getSaveCount() const { return fSaveCount; } |
| 1088 | unsigned int getSaveLayerCount() const { return fSaveLayerCount; } |
| 1089 | unsigned int getRestoreCount() const { return fRestoreCount; } |
| 1090 | |
| 1091 | private: |
| 1092 | unsigned int fSaveCount; |
| 1093 | unsigned int fSaveLayerCount; |
| 1094 | unsigned int fRestoreCount; |
| 1095 | |
| 1096 | typedef SkCanvas INHERITED; |
| 1097 | }; |
| 1098 | |
| skia.committer@gmail.com | 8e7d37d | 2014-05-28 03:06:06 +0000 | [diff] [blame] | 1099 | void check_save_state(skiatest::Reporter* reporter, SkPicture* picture, |
| commit-bot@chromium.org | 6d3eaea | 2014-05-27 23:41:45 +0000 | [diff] [blame] | 1100 | unsigned int numSaves, unsigned int numSaveLayers, |
| 1101 | unsigned int numRestores) { |
| 1102 | SaveCountingCanvas canvas(picture->width(), picture->height()); |
| 1103 | |
| 1104 | picture->draw(&canvas); |
| 1105 | |
| 1106 | REPORTER_ASSERT(reporter, numSaves == canvas.getSaveCount()); |
| 1107 | REPORTER_ASSERT(reporter, numSaveLayers == canvas.getSaveLayerCount()); |
| 1108 | REPORTER_ASSERT(reporter, numRestores == canvas.getRestoreCount()); |
| 1109 | } |
| 1110 | |
| 1111 | // This class exists so SkPicture can friend it and give it access to |
| 1112 | // the 'partialReplay' method. |
| 1113 | class SkPictureRecorderReplayTester { |
| 1114 | public: |
| 1115 | static SkPicture* Copy(SkPictureRecorder* recorder) { |
| 1116 | SkPictureRecorder recorder2; |
| 1117 | |
| robertphillips | 9f1c241 | 2014-06-09 06:25:34 -0700 | [diff] [blame] | 1118 | SkCanvas* canvas = recorder2.beginRecording(10, 10); |
| commit-bot@chromium.org | 6d3eaea | 2014-05-27 23:41:45 +0000 | [diff] [blame] | 1119 | |
| 1120 | recorder->partialReplay(canvas); |
| 1121 | |
| 1122 | return recorder2.endRecording(); |
| 1123 | } |
| 1124 | }; |
| 1125 | |
| robertphillips | 9058d60 | 2014-06-10 11:45:46 -0700 | [diff] [blame] | 1126 | static void create_imbalance(SkCanvas* canvas) { |
| 1127 | SkRect clipRect = SkRect::MakeWH(2, 2); |
| 1128 | SkRect drawRect = SkRect::MakeWH(10, 10); |
| 1129 | canvas->save(); |
| 1130 | canvas->clipRect(clipRect, SkRegion::kReplace_Op); |
| 1131 | canvas->translate(1.0f, 1.0f); |
| 1132 | SkPaint p; |
| 1133 | p.setColor(SK_ColorGREEN); |
| 1134 | canvas->drawRect(drawRect, p); |
| 1135 | // no restore |
| 1136 | } |
| 1137 | |
| 1138 | // This tests that replaying a potentially unbalanced picture into a canvas |
| 1139 | // doesn't affect the canvas' save count or matrix/clip state. |
| 1140 | static void check_balance(skiatest::Reporter* reporter, SkPicture* picture) { |
| 1141 | SkBitmap bm; |
| 1142 | bm.allocN32Pixels(4, 3); |
| 1143 | SkCanvas canvas(bm); |
| 1144 | |
| 1145 | int beforeSaveCount = canvas.getSaveCount(); |
| 1146 | |
| 1147 | SkMatrix beforeMatrix = canvas.getTotalMatrix(); |
| 1148 | |
| 1149 | SkRect beforeClip; |
| 1150 | |
| 1151 | canvas.getClipBounds(&beforeClip); |
| 1152 | |
| 1153 | canvas.drawPicture(picture); |
| 1154 | |
| 1155 | REPORTER_ASSERT(reporter, beforeSaveCount == canvas.getSaveCount()); |
| 1156 | REPORTER_ASSERT(reporter, beforeMatrix == canvas.getTotalMatrix()); |
| 1157 | |
| 1158 | SkRect afterClip; |
| 1159 | |
| 1160 | canvas.getClipBounds(&afterClip); |
| 1161 | |
| 1162 | REPORTER_ASSERT(reporter, afterClip == beforeClip); |
| 1163 | } |
| 1164 | |
| commit-bot@chromium.org | 6d3eaea | 2014-05-27 23:41:45 +0000 | [diff] [blame] | 1165 | // Test out SkPictureRecorder::partialReplay |
| 1166 | DEF_TEST(PictureRecorder_replay, reporter) { |
| 1167 | // check save/saveLayer state |
| 1168 | { |
| 1169 | SkPictureRecorder recorder; |
| 1170 | |
| robertphillips | 9f1c241 | 2014-06-09 06:25:34 -0700 | [diff] [blame] | 1171 | SkCanvas* canvas = recorder.beginRecording(10, 10); |
| commit-bot@chromium.org | 6d3eaea | 2014-05-27 23:41:45 +0000 | [diff] [blame] | 1172 | |
| 1173 | canvas->saveLayer(NULL, NULL); |
| 1174 | |
| 1175 | SkAutoTUnref<SkPicture> copy(SkPictureRecorderReplayTester::Copy(&recorder)); |
| 1176 | |
| 1177 | // The extra save and restore comes from the Copy process. |
| 1178 | check_save_state(reporter, copy, 2, 1, 3); |
| 1179 | |
| 1180 | canvas->saveLayer(NULL, NULL); |
| 1181 | |
| 1182 | SkAutoTUnref<SkPicture> final(recorder.endRecording()); |
| 1183 | |
| 1184 | check_save_state(reporter, final, 1, 2, 3); |
| 1185 | |
| 1186 | // The copy shouldn't pick up any operations added after it was made |
| 1187 | check_save_state(reporter, copy, 2, 1, 3); |
| 1188 | } |
| 1189 | |
| 1190 | // (partially) check leakage of draw ops |
| 1191 | { |
| 1192 | SkPictureRecorder recorder; |
| 1193 | |
| robertphillips | 9f1c241 | 2014-06-09 06:25:34 -0700 | [diff] [blame] | 1194 | SkCanvas* canvas = recorder.beginRecording(10, 10); |
| commit-bot@chromium.org | 6d3eaea | 2014-05-27 23:41:45 +0000 | [diff] [blame] | 1195 | |
| 1196 | SkRect r = SkRect::MakeWH(5, 5); |
| 1197 | SkPaint p; |
| 1198 | |
| 1199 | canvas->drawRect(r, p); |
| 1200 | |
| 1201 | SkAutoTUnref<SkPicture> copy(SkPictureRecorderReplayTester::Copy(&recorder)); |
| 1202 | |
| 1203 | REPORTER_ASSERT(reporter, !copy->willPlayBackBitmaps()); |
| 1204 | |
| 1205 | SkBitmap bm; |
| 1206 | make_bm(&bm, 10, 10, SK_ColorRED, true); |
| 1207 | |
| 1208 | r.offset(5.0f, 5.0f); |
| 1209 | canvas->drawBitmapRectToRect(bm, NULL, r); |
| 1210 | |
| 1211 | SkAutoTUnref<SkPicture> final(recorder.endRecording()); |
| 1212 | REPORTER_ASSERT(reporter, final->willPlayBackBitmaps()); |
| 1213 | |
| 1214 | REPORTER_ASSERT(reporter, copy->uniqueID() != final->uniqueID()); |
| 1215 | |
| 1216 | // The snapshot shouldn't pick up any operations added after it was made |
| 1217 | REPORTER_ASSERT(reporter, !copy->willPlayBackBitmaps()); |
| 1218 | } |
| robertphillips | 9058d60 | 2014-06-10 11:45:46 -0700 | [diff] [blame] | 1219 | |
| 1220 | // Recreate the Android partialReplay test case |
| 1221 | { |
| 1222 | SkPictureRecorder recorder; |
| 1223 | |
| 1224 | SkCanvas* canvas = recorder.beginRecording(4, 3, NULL, 0); |
| 1225 | create_imbalance(canvas); |
| 1226 | |
| 1227 | int expectedSaveCount = canvas->getSaveCount(); |
| 1228 | |
| 1229 | SkAutoTUnref<SkPicture> copy(SkPictureRecorderReplayTester::Copy(&recorder)); |
| 1230 | check_balance(reporter, copy); |
| 1231 | |
| 1232 | REPORTER_ASSERT(reporter, expectedSaveCount = canvas->getSaveCount()); |
| 1233 | |
| 1234 | // End the recording of source to test the picture finalization |
| 1235 | // process isn't complicated by the partialReplay step |
| 1236 | SkAutoTUnref<SkPicture> final(recorder.endRecording()); |
| 1237 | } |
| commit-bot@chromium.org | 6d3eaea | 2014-05-27 23:41:45 +0000 | [diff] [blame] | 1238 | } |
| commit-bot@chromium.org | 6d3eaea | 2014-05-27 23:41:45 +0000 | [diff] [blame] | 1239 | |
| commit-bot@chromium.org | ea7d08e | 2014-02-13 16:00:51 +0000 | [diff] [blame] | 1240 | static void test_unbalanced_save_restores(skiatest::Reporter* reporter) { |
| 1241 | SkCanvas testCanvas(100, 100); |
| 1242 | set_canvas_to_save_count_4(&testCanvas); |
| 1243 | |
| 1244 | REPORTER_ASSERT(reporter, 4 == testCanvas.getSaveCount()); |
| 1245 | |
| 1246 | SkPaint paint; |
| 1247 | SkRect rect = SkRect::MakeLTRB(-10000000, -10000000, 10000000, 10000000); |
| 1248 | |
| robertphillips@google.com | 84b18c7 | 2014-04-13 19:09:42 +0000 | [diff] [blame] | 1249 | SkPictureRecorder recorder; |
| commit-bot@chromium.org | ea7d08e | 2014-02-13 16:00:51 +0000 | [diff] [blame] | 1250 | |
| robertphillips@google.com | 84b18c7 | 2014-04-13 19:09:42 +0000 | [diff] [blame] | 1251 | { |
| 1252 | // Create picture with 2 unbalanced saves |
| robertphillips | 9f1c241 | 2014-06-09 06:25:34 -0700 | [diff] [blame] | 1253 | SkCanvas* canvas = recorder.beginRecording(100, 100); |
| robertphillips@google.com | 84b18c7 | 2014-04-13 19:09:42 +0000 | [diff] [blame] | 1254 | canvas->save(); |
| 1255 | canvas->translate(10, 10); |
| 1256 | canvas->drawRect(rect, paint); |
| 1257 | canvas->save(); |
| 1258 | canvas->translate(10, 10); |
| 1259 | canvas->drawRect(rect, paint); |
| 1260 | SkAutoTUnref<SkPicture> extraSavePicture(recorder.endRecording()); |
| 1261 | |
| robertphillips | 9b14f26 | 2014-06-04 05:40:44 -0700 | [diff] [blame] | 1262 | testCanvas.drawPicture(extraSavePicture); |
| robertphillips@google.com | 84b18c7 | 2014-04-13 19:09:42 +0000 | [diff] [blame] | 1263 | REPORTER_ASSERT(reporter, 4 == testCanvas.getSaveCount()); |
| 1264 | } |
| commit-bot@chromium.org | ea7d08e | 2014-02-13 16:00:51 +0000 | [diff] [blame] | 1265 | |
| 1266 | set_canvas_to_save_count_4(&testCanvas); |
| 1267 | |
| robertphillips@google.com | 84b18c7 | 2014-04-13 19:09:42 +0000 | [diff] [blame] | 1268 | { |
| 1269 | // Create picture with 2 unbalanced restores |
| robertphillips | 9f1c241 | 2014-06-09 06:25:34 -0700 | [diff] [blame] | 1270 | SkCanvas* canvas = recorder.beginRecording(100, 100); |
| robertphillips@google.com | 84b18c7 | 2014-04-13 19:09:42 +0000 | [diff] [blame] | 1271 | canvas->save(); |
| 1272 | canvas->translate(10, 10); |
| 1273 | canvas->drawRect(rect, paint); |
| 1274 | canvas->save(); |
| 1275 | canvas->translate(10, 10); |
| 1276 | canvas->drawRect(rect, paint); |
| 1277 | canvas->restore(); |
| 1278 | canvas->restore(); |
| 1279 | canvas->restore(); |
| 1280 | canvas->restore(); |
| 1281 | SkAutoTUnref<SkPicture> extraRestorePicture(recorder.endRecording()); |
| commit-bot@chromium.org | ea7d08e | 2014-02-13 16:00:51 +0000 | [diff] [blame] | 1282 | |
| robertphillips | 9b14f26 | 2014-06-04 05:40:44 -0700 | [diff] [blame] | 1283 | testCanvas.drawPicture(extraRestorePicture); |
| robertphillips@google.com | 84b18c7 | 2014-04-13 19:09:42 +0000 | [diff] [blame] | 1284 | REPORTER_ASSERT(reporter, 4 == testCanvas.getSaveCount()); |
| 1285 | } |
| commit-bot@chromium.org | ea7d08e | 2014-02-13 16:00:51 +0000 | [diff] [blame] | 1286 | |
| robertphillips@google.com | 84b18c7 | 2014-04-13 19:09:42 +0000 | [diff] [blame] | 1287 | set_canvas_to_save_count_4(&testCanvas); |
| commit-bot@chromium.org | ea7d08e | 2014-02-13 16:00:51 +0000 | [diff] [blame] | 1288 | |
| robertphillips@google.com | 84b18c7 | 2014-04-13 19:09:42 +0000 | [diff] [blame] | 1289 | { |
| robertphillips | 9f1c241 | 2014-06-09 06:25:34 -0700 | [diff] [blame] | 1290 | SkCanvas* canvas = recorder.beginRecording(100, 100); |
| robertphillips@google.com | 84b18c7 | 2014-04-13 19:09:42 +0000 | [diff] [blame] | 1291 | canvas->translate(10, 10); |
| 1292 | canvas->drawRect(rect, paint); |
| 1293 | SkAutoTUnref<SkPicture> noSavePicture(recorder.endRecording()); |
| 1294 | |
| robertphillips | 9b14f26 | 2014-06-04 05:40:44 -0700 | [diff] [blame] | 1295 | testCanvas.drawPicture(noSavePicture); |
| robertphillips@google.com | 84b18c7 | 2014-04-13 19:09:42 +0000 | [diff] [blame] | 1296 | REPORTER_ASSERT(reporter, 4 == testCanvas.getSaveCount()); |
| 1297 | REPORTER_ASSERT(reporter, testCanvas.getTotalMatrix().isIdentity()); |
| 1298 | } |
| commit-bot@chromium.org | ea7d08e | 2014-02-13 16:00:51 +0000 | [diff] [blame] | 1299 | } |
| 1300 | |
| sugoi@google.com | 54f0d1b | 2013-02-27 19:17:41 +0000 | [diff] [blame] | 1301 | static void test_peephole() { |
| commit-bot@chromium.org | e0e7cfe | 2013-09-09 20:09:12 +0000 | [diff] [blame] | 1302 | SkRandom rand; |
| reed@google.com | 21b519d | 2012-10-02 17:42:15 +0000 | [diff] [blame] | 1303 | |
| robertphillips@google.com | 84b18c7 | 2014-04-13 19:09:42 +0000 | [diff] [blame] | 1304 | SkPictureRecorder recorder; |
| 1305 | |
| reed@google.com | 21b519d | 2012-10-02 17:42:15 +0000 | [diff] [blame] | 1306 | for (int j = 0; j < 100; j++) { |
| commit-bot@chromium.org | e0e7cfe | 2013-09-09 20:09:12 +0000 | [diff] [blame] | 1307 | SkRandom rand2(rand); // remember the seed |
| reed@google.com | 21b519d | 2012-10-02 17:42:15 +0000 | [diff] [blame] | 1308 | |
| robertphillips | 9f1c241 | 2014-06-09 06:25:34 -0700 | [diff] [blame] | 1309 | SkCanvas* canvas = recorder.beginRecording(100, 100); |
| reed@google.com | 21b519d | 2012-10-02 17:42:15 +0000 | [diff] [blame] | 1310 | |
| 1311 | for (int i = 0; i < 1000; ++i) { |
| 1312 | rand_op(canvas, rand); |
| 1313 | } |
| robertphillips@google.com | 84b18c7 | 2014-04-13 19:09:42 +0000 | [diff] [blame] | 1314 | SkAutoTUnref<SkPicture> picture(recorder.endRecording()); |
| jvanverth@google.com | c490f80 | 2013-03-04 13:56:38 +0000 | [diff] [blame] | 1315 | |
| 1316 | rand = rand2; |
| reed@google.com | 21b519d | 2012-10-02 17:42:15 +0000 | [diff] [blame] | 1317 | } |
| 1318 | |
| 1319 | { |
| robertphillips | 9f1c241 | 2014-06-09 06:25:34 -0700 | [diff] [blame] | 1320 | SkCanvas* canvas = recorder.beginRecording(100, 100); |
| reed@google.com | 21b519d | 2012-10-02 17:42:15 +0000 | [diff] [blame] | 1321 | SkRect rect = SkRect::MakeWH(50, 50); |
| skia.committer@gmail.com | 52c2437 | 2012-10-03 02:01:13 +0000 | [diff] [blame] | 1322 | |
| reed@google.com | 21b519d | 2012-10-02 17:42:15 +0000 | [diff] [blame] | 1323 | for (int i = 0; i < 100; ++i) { |
| 1324 | canvas->save(); |
| 1325 | } |
| 1326 | while (canvas->getSaveCount() > 1) { |
| 1327 | canvas->clipRect(rect); |
| 1328 | canvas->restore(); |
| 1329 | } |
| robertphillips@google.com | 84b18c7 | 2014-04-13 19:09:42 +0000 | [diff] [blame] | 1330 | SkAutoTUnref<SkPicture> picture(recorder.endRecording()); |
| reed@google.com | 21b519d | 2012-10-02 17:42:15 +0000 | [diff] [blame] | 1331 | } |
| 1332 | } |
| 1333 | |
| scroggo@google.com | 4b90b11 | 2012-12-04 15:08:56 +0000 | [diff] [blame] | 1334 | #ifndef SK_DEBUG |
| 1335 | // Only test this is in release mode. We deliberately crash in debug mode, since a valid caller |
| 1336 | // should never do this. |
| 1337 | static void test_bad_bitmap() { |
| 1338 | // This bitmap has a width and height but no pixels. As a result, attempting to record it will |
| 1339 | // fail. |
| 1340 | SkBitmap bm; |
| commit-bot@chromium.org | a3264e5 | 2014-05-30 13:26:10 +0000 | [diff] [blame] | 1341 | bm.setInfo(SkImageInfo::MakeN32Premul(100, 100)); |
| robertphillips@google.com | 84b18c7 | 2014-04-13 19:09:42 +0000 | [diff] [blame] | 1342 | SkPictureRecorder recorder; |
| robertphillips | 9f1c241 | 2014-06-09 06:25:34 -0700 | [diff] [blame] | 1343 | SkCanvas* recordingCanvas = recorder.beginRecording(100, 100); |
| scroggo@google.com | 4b90b11 | 2012-12-04 15:08:56 +0000 | [diff] [blame] | 1344 | recordingCanvas->drawBitmap(bm, 0, 0); |
| robertphillips@google.com | 84b18c7 | 2014-04-13 19:09:42 +0000 | [diff] [blame] | 1345 | SkAutoTUnref<SkPicture> picture(recorder.endRecording()); |
| scroggo@google.com | 4b90b11 | 2012-12-04 15:08:56 +0000 | [diff] [blame] | 1346 | |
| 1347 | SkCanvas canvas; |
| robertphillips | 9b14f26 | 2014-06-04 05:40:44 -0700 | [diff] [blame] | 1348 | canvas.drawPicture(picture); |
| scroggo@google.com | 4b90b11 | 2012-12-04 15:08:56 +0000 | [diff] [blame] | 1349 | } |
| 1350 | #endif |
| 1351 | |
| reed@google.com | 672588b | 2014-01-08 15:42:01 +0000 | [diff] [blame] | 1352 | static SkData* encode_bitmap_to_data(size_t*, const SkBitmap& bm) { |
| scroggo@google.com | 1b1bcc3 | 2013-05-21 20:31:23 +0000 | [diff] [blame] | 1353 | return SkImageEncoder::EncodeData(bm, SkImageEncoder::kPNG_Type, 100); |
| scroggo@google.com | 7c9d539 | 2012-12-10 15:40:55 +0000 | [diff] [blame] | 1354 | } |
| 1355 | |
| 1356 | static SkData* serialized_picture_from_bitmap(const SkBitmap& bitmap) { |
| robertphillips@google.com | 84b18c7 | 2014-04-13 19:09:42 +0000 | [diff] [blame] | 1357 | SkPictureRecorder recorder; |
| robertphillips | 9f1c241 | 2014-06-09 06:25:34 -0700 | [diff] [blame] | 1358 | SkCanvas* canvas = recorder.beginRecording(bitmap.width(), bitmap.height()); |
| scroggo@google.com | 7c9d539 | 2012-12-10 15:40:55 +0000 | [diff] [blame] | 1359 | canvas->drawBitmap(bitmap, 0, 0); |
| robertphillips@google.com | 84b18c7 | 2014-04-13 19:09:42 +0000 | [diff] [blame] | 1360 | SkAutoTUnref<SkPicture> picture(recorder.endRecording()); |
| 1361 | |
| scroggo@google.com | 7c9d539 | 2012-12-10 15:40:55 +0000 | [diff] [blame] | 1362 | SkDynamicMemoryWStream wStream; |
| robertphillips@google.com | 84b18c7 | 2014-04-13 19:09:42 +0000 | [diff] [blame] | 1363 | picture->serialize(&wStream, &encode_bitmap_to_data); |
| scroggo@google.com | 7c9d539 | 2012-12-10 15:40:55 +0000 | [diff] [blame] | 1364 | return wStream.copyToData(); |
| 1365 | } |
| 1366 | |
| scroggo@google.com | 49ce11b | 2013-04-25 18:29:32 +0000 | [diff] [blame] | 1367 | struct ErrorContext { |
| 1368 | int fErrors; |
| 1369 | skiatest::Reporter* fReporter; |
| 1370 | }; |
| 1371 | |
| 1372 | static void assert_one_parse_error_cb(SkError error, void* context) { |
| 1373 | ErrorContext* errorContext = static_cast<ErrorContext*>(context); |
| 1374 | errorContext->fErrors++; |
| 1375 | // This test only expects one error, and that is a kParseError. If there are others, |
| 1376 | // there is some unknown problem. |
| 1377 | REPORTER_ASSERT_MESSAGE(errorContext->fReporter, 1 == errorContext->fErrors, |
| 1378 | "This threw more errors than expected."); |
| 1379 | REPORTER_ASSERT_MESSAGE(errorContext->fReporter, kParseError_SkError == error, |
| 1380 | SkGetLastErrorString()); |
| 1381 | } |
| 1382 | |
| scroggo@google.com | 7c9d539 | 2012-12-10 15:40:55 +0000 | [diff] [blame] | 1383 | static void test_bitmap_with_encoded_data(skiatest::Reporter* reporter) { |
| 1384 | // Create a bitmap that will be encoded. |
| 1385 | SkBitmap original; |
| 1386 | make_bm(&original, 100, 100, SK_ColorBLUE, true); |
| 1387 | SkDynamicMemoryWStream wStream; |
| 1388 | if (!SkImageEncoder::EncodeStream(&wStream, original, SkImageEncoder::kPNG_Type, 100)) { |
| 1389 | return; |
| 1390 | } |
| 1391 | SkAutoDataUnref data(wStream.copyToData()); |
| scroggo@google.com | 7c9d539 | 2012-12-10 15:40:55 +0000 | [diff] [blame] | 1392 | |
| scroggo@google.com | 7c9d539 | 2012-12-10 15:40:55 +0000 | [diff] [blame] | 1393 | SkBitmap bm; |
| halcanary@google.com | 3d50ea1 | 2014-01-02 13:15:13 +0000 | [diff] [blame] | 1394 | bool installSuccess = SkInstallDiscardablePixelRef( |
| commit-bot@chromium.org | 2d970b5 | 2014-05-27 14:14:22 +0000 | [diff] [blame] | 1395 | SkDecodingImageGenerator::Create(data, SkDecodingImageGenerator::Options()), &bm); |
| reed@google.com | bf79023 | 2013-12-13 19:45:58 +0000 | [diff] [blame] | 1396 | REPORTER_ASSERT(reporter, installSuccess); |
| scroggo@google.com | 7c9d539 | 2012-12-10 15:40:55 +0000 | [diff] [blame] | 1397 | |
| 1398 | // Write both bitmaps to pictures, and ensure that the resulting data streams are the same. |
| 1399 | // Flattening original will follow the old path of performing an encode, while flattening bm |
| 1400 | // will use the already encoded data. |
| 1401 | SkAutoDataUnref picture1(serialized_picture_from_bitmap(original)); |
| 1402 | SkAutoDataUnref picture2(serialized_picture_from_bitmap(bm)); |
| 1403 | REPORTER_ASSERT(reporter, picture1->equals(picture2)); |
| scroggo@google.com | 49ce11b | 2013-04-25 18:29:32 +0000 | [diff] [blame] | 1404 | // Now test that a parse error was generated when trying to create a new SkPicture without |
| 1405 | // providing a function to decode the bitmap. |
| 1406 | ErrorContext context; |
| 1407 | context.fErrors = 0; |
| 1408 | context.fReporter = reporter; |
| 1409 | SkSetErrorCallback(assert_one_parse_error_cb, &context); |
| 1410 | SkMemoryStream pictureStream(picture1); |
| scroggo@google.com | 49ce11b | 2013-04-25 18:29:32 +0000 | [diff] [blame] | 1411 | SkClearLastError(); |
| scroggo@google.com | f1754ec | 2013-06-28 21:32:00 +0000 | [diff] [blame] | 1412 | SkAutoUnref pictureFromStream(SkPicture::CreateFromStream(&pictureStream, NULL)); |
| 1413 | REPORTER_ASSERT(reporter, pictureFromStream.get() != NULL); |
| scroggo@google.com | 49ce11b | 2013-04-25 18:29:32 +0000 | [diff] [blame] | 1414 | SkClearLastError(); |
| 1415 | SkSetErrorCallback(NULL, NULL); |
| scroggo@google.com | 7c9d539 | 2012-12-10 15:40:55 +0000 | [diff] [blame] | 1416 | } |
| 1417 | |
| commit-bot@chromium.org | 70512af | 2014-03-18 17:45:32 +0000 | [diff] [blame] | 1418 | static void test_draw_empty(skiatest::Reporter* reporter) { |
| 1419 | SkBitmap result; |
| 1420 | make_bm(&result, 2, 2, SK_ColorBLACK, false); |
| 1421 | |
| 1422 | SkCanvas canvas(result); |
| 1423 | |
| 1424 | { |
| 1425 | // stock SkPicture |
| robertphillips@google.com | 84b18c7 | 2014-04-13 19:09:42 +0000 | [diff] [blame] | 1426 | SkPictureRecorder recorder; |
| robertphillips | 9f1c241 | 2014-06-09 06:25:34 -0700 | [diff] [blame] | 1427 | recorder.beginRecording(1, 1); |
| robertphillips@google.com | 84b18c7 | 2014-04-13 19:09:42 +0000 | [diff] [blame] | 1428 | SkAutoTUnref<SkPicture> picture(recorder.endRecording()); |
| commit-bot@chromium.org | 70512af | 2014-03-18 17:45:32 +0000 | [diff] [blame] | 1429 | |
| robertphillips | 9b14f26 | 2014-06-04 05:40:44 -0700 | [diff] [blame] | 1430 | canvas.drawPicture(picture); |
| commit-bot@chromium.org | 70512af | 2014-03-18 17:45:32 +0000 | [diff] [blame] | 1431 | } |
| 1432 | |
| 1433 | { |
| 1434 | // tile grid |
| commit-bot@chromium.org | 5fb2ce3 | 2014-04-17 23:35:06 +0000 | [diff] [blame] | 1435 | SkTileGridFactory::TileGridInfo gridInfo; |
| commit-bot@chromium.org | 70512af | 2014-03-18 17:45:32 +0000 | [diff] [blame] | 1436 | gridInfo.fMargin.setEmpty(); |
| 1437 | gridInfo.fOffset.setZero(); |
| 1438 | gridInfo.fTileInterval.set(1, 1); |
| 1439 | |
| commit-bot@chromium.org | 5fb2ce3 | 2014-04-17 23:35:06 +0000 | [diff] [blame] | 1440 | SkTileGridFactory factory(gridInfo); |
| 1441 | SkPictureRecorder recorder; |
| robertphillips | 9f1c241 | 2014-06-09 06:25:34 -0700 | [diff] [blame] | 1442 | recorder.beginRecording(1, 1, &factory); |
| robertphillips@google.com | 84b18c7 | 2014-04-13 19:09:42 +0000 | [diff] [blame] | 1443 | SkAutoTUnref<SkPicture> picture(recorder.endRecording()); |
| commit-bot@chromium.org | 70512af | 2014-03-18 17:45:32 +0000 | [diff] [blame] | 1444 | |
| robertphillips | 9b14f26 | 2014-06-04 05:40:44 -0700 | [diff] [blame] | 1445 | canvas.drawPicture(picture); |
| commit-bot@chromium.org | 70512af | 2014-03-18 17:45:32 +0000 | [diff] [blame] | 1446 | } |
| 1447 | |
| 1448 | { |
| 1449 | // RTree |
| commit-bot@chromium.org | 5fb2ce3 | 2014-04-17 23:35:06 +0000 | [diff] [blame] | 1450 | SkRTreeFactory factory; |
| 1451 | SkPictureRecorder recorder; |
| robertphillips | 9f1c241 | 2014-06-09 06:25:34 -0700 | [diff] [blame] | 1452 | recorder.beginRecording(1, 1, &factory); |
| robertphillips@google.com | 84b18c7 | 2014-04-13 19:09:42 +0000 | [diff] [blame] | 1453 | SkAutoTUnref<SkPicture> picture(recorder.endRecording()); |
| commit-bot@chromium.org | 70512af | 2014-03-18 17:45:32 +0000 | [diff] [blame] | 1454 | |
| robertphillips | 9b14f26 | 2014-06-04 05:40:44 -0700 | [diff] [blame] | 1455 | canvas.drawPicture(picture); |
| commit-bot@chromium.org | 70512af | 2014-03-18 17:45:32 +0000 | [diff] [blame] | 1456 | } |
| 1457 | |
| 1458 | { |
| 1459 | // quad tree |
| commit-bot@chromium.org | 5fb2ce3 | 2014-04-17 23:35:06 +0000 | [diff] [blame] | 1460 | SkQuadTreeFactory factory; |
| 1461 | SkPictureRecorder recorder; |
| robertphillips | 9f1c241 | 2014-06-09 06:25:34 -0700 | [diff] [blame] | 1462 | recorder.beginRecording(1, 1, &factory); |
| robertphillips@google.com | 84b18c7 | 2014-04-13 19:09:42 +0000 | [diff] [blame] | 1463 | SkAutoTUnref<SkPicture> picture(recorder.endRecording()); |
| commit-bot@chromium.org | 70512af | 2014-03-18 17:45:32 +0000 | [diff] [blame] | 1464 | |
| robertphillips | 9b14f26 | 2014-06-04 05:40:44 -0700 | [diff] [blame] | 1465 | canvas.drawPicture(picture); |
| commit-bot@chromium.org | 70512af | 2014-03-18 17:45:32 +0000 | [diff] [blame] | 1466 | } |
| 1467 | } |
| 1468 | |
| junov@chromium.org | d575eed | 2013-05-08 15:39:13 +0000 | [diff] [blame] | 1469 | static void test_clip_bound_opt(skiatest::Reporter* reporter) { |
| 1470 | // Test for crbug.com/229011 |
| 1471 | SkRect rect1 = SkRect::MakeXYWH(SkIntToScalar(4), SkIntToScalar(4), |
| 1472 | SkIntToScalar(2), SkIntToScalar(2)); |
| 1473 | SkRect rect2 = SkRect::MakeXYWH(SkIntToScalar(7), SkIntToScalar(7), |
| 1474 | SkIntToScalar(1), SkIntToScalar(1)); |
| 1475 | SkRect rect3 = SkRect::MakeXYWH(SkIntToScalar(6), SkIntToScalar(6), |
| 1476 | SkIntToScalar(1), SkIntToScalar(1)); |
| 1477 | |
| 1478 | SkPath invPath; |
| 1479 | invPath.addOval(rect1); |
| 1480 | invPath.setFillType(SkPath::kInverseEvenOdd_FillType); |
| 1481 | SkPath path; |
| 1482 | path.addOval(rect2); |
| 1483 | SkPath path2; |
| 1484 | path2.addOval(rect3); |
| 1485 | SkIRect clipBounds; |
| robertphillips@google.com | 84b18c7 | 2014-04-13 19:09:42 +0000 | [diff] [blame] | 1486 | SkPictureRecorder recorder; |
| junov@chromium.org | d575eed | 2013-05-08 15:39:13 +0000 | [diff] [blame] | 1487 | // Minimalist test set for 100% code coverage of |
| 1488 | // SkPictureRecord::updateClipConservativelyUsingBounds |
| 1489 | { |
| robertphillips | 9f1c241 | 2014-06-09 06:25:34 -0700 | [diff] [blame] | 1490 | SkCanvas* canvas = recorder.beginRecording(10, 10); |
| junov@chromium.org | d575eed | 2013-05-08 15:39:13 +0000 | [diff] [blame] | 1491 | canvas->clipPath(invPath, SkRegion::kIntersect_Op); |
| 1492 | bool nonEmpty = canvas->getClipDeviceBounds(&clipBounds); |
| 1493 | REPORTER_ASSERT(reporter, true == nonEmpty); |
| 1494 | REPORTER_ASSERT(reporter, 0 == clipBounds.fLeft); |
| 1495 | REPORTER_ASSERT(reporter, 0 == clipBounds.fTop); |
| 1496 | REPORTER_ASSERT(reporter, 10 == clipBounds.fBottom); |
| 1497 | REPORTER_ASSERT(reporter, 10 == clipBounds.fRight); |
| 1498 | } |
| 1499 | { |
| robertphillips | 9f1c241 | 2014-06-09 06:25:34 -0700 | [diff] [blame] | 1500 | SkCanvas* canvas = recorder.beginRecording(10, 10); |
| junov@chromium.org | d575eed | 2013-05-08 15:39:13 +0000 | [diff] [blame] | 1501 | canvas->clipPath(path, SkRegion::kIntersect_Op); |
| 1502 | canvas->clipPath(invPath, SkRegion::kIntersect_Op); |
| 1503 | bool nonEmpty = canvas->getClipDeviceBounds(&clipBounds); |
| 1504 | REPORTER_ASSERT(reporter, true == nonEmpty); |
| 1505 | REPORTER_ASSERT(reporter, 7 == clipBounds.fLeft); |
| 1506 | REPORTER_ASSERT(reporter, 7 == clipBounds.fTop); |
| 1507 | REPORTER_ASSERT(reporter, 8 == clipBounds.fBottom); |
| 1508 | REPORTER_ASSERT(reporter, 8 == clipBounds.fRight); |
| 1509 | } |
| 1510 | { |
| robertphillips | 9f1c241 | 2014-06-09 06:25:34 -0700 | [diff] [blame] | 1511 | SkCanvas* canvas = recorder.beginRecording(10, 10); |
| junov@chromium.org | d575eed | 2013-05-08 15:39:13 +0000 | [diff] [blame] | 1512 | canvas->clipPath(path, SkRegion::kIntersect_Op); |
| 1513 | canvas->clipPath(invPath, SkRegion::kUnion_Op); |
| 1514 | bool nonEmpty = canvas->getClipDeviceBounds(&clipBounds); |
| 1515 | REPORTER_ASSERT(reporter, true == nonEmpty); |
| 1516 | REPORTER_ASSERT(reporter, 0 == clipBounds.fLeft); |
| 1517 | REPORTER_ASSERT(reporter, 0 == clipBounds.fTop); |
| 1518 | REPORTER_ASSERT(reporter, 10 == clipBounds.fBottom); |
| 1519 | REPORTER_ASSERT(reporter, 10 == clipBounds.fRight); |
| 1520 | } |
| 1521 | { |
| robertphillips | 9f1c241 | 2014-06-09 06:25:34 -0700 | [diff] [blame] | 1522 | SkCanvas* canvas = recorder.beginRecording(10, 10); |
| junov@chromium.org | d575eed | 2013-05-08 15:39:13 +0000 | [diff] [blame] | 1523 | canvas->clipPath(path, SkRegion::kDifference_Op); |
| 1524 | bool nonEmpty = canvas->getClipDeviceBounds(&clipBounds); |
| 1525 | REPORTER_ASSERT(reporter, true == nonEmpty); |
| 1526 | REPORTER_ASSERT(reporter, 0 == clipBounds.fLeft); |
| 1527 | REPORTER_ASSERT(reporter, 0 == clipBounds.fTop); |
| 1528 | REPORTER_ASSERT(reporter, 10 == clipBounds.fBottom); |
| 1529 | REPORTER_ASSERT(reporter, 10 == clipBounds.fRight); |
| 1530 | } |
| 1531 | { |
| robertphillips | 9f1c241 | 2014-06-09 06:25:34 -0700 | [diff] [blame] | 1532 | SkCanvas* canvas = recorder.beginRecording(10, 10); |
| junov@chromium.org | d575eed | 2013-05-08 15:39:13 +0000 | [diff] [blame] | 1533 | canvas->clipPath(path, SkRegion::kReverseDifference_Op); |
| 1534 | bool nonEmpty = canvas->getClipDeviceBounds(&clipBounds); |
| 1535 | // True clip is actually empty in this case, but the best |
| 1536 | // determination we can make using only bounds as input is that the |
| 1537 | // clip is included in the bounds of 'path'. |
| 1538 | REPORTER_ASSERT(reporter, true == nonEmpty); |
| 1539 | REPORTER_ASSERT(reporter, 7 == clipBounds.fLeft); |
| 1540 | REPORTER_ASSERT(reporter, 7 == clipBounds.fTop); |
| 1541 | REPORTER_ASSERT(reporter, 8 == clipBounds.fBottom); |
| 1542 | REPORTER_ASSERT(reporter, 8 == clipBounds.fRight); |
| 1543 | } |
| 1544 | { |
| robertphillips | 9f1c241 | 2014-06-09 06:25:34 -0700 | [diff] [blame] | 1545 | SkCanvas* canvas = recorder.beginRecording(10, 10); |
| junov@chromium.org | d575eed | 2013-05-08 15:39:13 +0000 | [diff] [blame] | 1546 | canvas->clipPath(path, SkRegion::kIntersect_Op); |
| 1547 | canvas->clipPath(path2, SkRegion::kXOR_Op); |
| 1548 | bool nonEmpty = canvas->getClipDeviceBounds(&clipBounds); |
| 1549 | REPORTER_ASSERT(reporter, true == nonEmpty); |
| 1550 | REPORTER_ASSERT(reporter, 6 == clipBounds.fLeft); |
| 1551 | REPORTER_ASSERT(reporter, 6 == clipBounds.fTop); |
| 1552 | REPORTER_ASSERT(reporter, 8 == clipBounds.fBottom); |
| 1553 | REPORTER_ASSERT(reporter, 8 == clipBounds.fRight); |
| 1554 | } |
| 1555 | } |
| 1556 | |
| fmalita@google.com | d0f1a4f | 2013-08-27 15:50:19 +0000 | [diff] [blame] | 1557 | /** |
| 1558 | * A canvas that records the number of clip commands. |
| 1559 | */ |
| 1560 | class ClipCountingCanvas : public SkCanvas { |
| 1561 | public: |
| commit-bot@chromium.org | 6d3eaea | 2014-05-27 23:41:45 +0000 | [diff] [blame] | 1562 | ClipCountingCanvas(int width, int height) |
| commit-bot@chromium.org | e254310 | 2014-01-31 19:42:58 +0000 | [diff] [blame] | 1563 | : INHERITED(width, height) |
| fmalita@google.com | d0f1a4f | 2013-08-27 15:50:19 +0000 | [diff] [blame] | 1564 | , fClipCount(0){ |
| 1565 | } |
| 1566 | |
| skia.committer@gmail.com | 370a899 | 2014-03-01 03:02:09 +0000 | [diff] [blame] | 1567 | virtual void onClipRect(const SkRect& r, |
| 1568 | SkRegion::Op op, |
| robertphillips@google.com | 8f90a89 | 2014-02-28 18:19:39 +0000 | [diff] [blame] | 1569 | ClipEdgeStyle edgeStyle) SK_OVERRIDE { |
| fmalita@google.com | d0f1a4f | 2013-08-27 15:50:19 +0000 | [diff] [blame] | 1570 | fClipCount += 1; |
| robertphillips@google.com | 8f90a89 | 2014-02-28 18:19:39 +0000 | [diff] [blame] | 1571 | this->INHERITED::onClipRect(r, op, edgeStyle); |
| fmalita@google.com | d0f1a4f | 2013-08-27 15:50:19 +0000 | [diff] [blame] | 1572 | } |
| 1573 | |
| skia.committer@gmail.com | 370a899 | 2014-03-01 03:02:09 +0000 | [diff] [blame] | 1574 | virtual void onClipRRect(const SkRRect& rrect, |
| 1575 | SkRegion::Op op, |
| robertphillips@google.com | 8f90a89 | 2014-02-28 18:19:39 +0000 | [diff] [blame] | 1576 | ClipEdgeStyle edgeStyle)SK_OVERRIDE { |
| fmalita@google.com | d0f1a4f | 2013-08-27 15:50:19 +0000 | [diff] [blame] | 1577 | fClipCount += 1; |
| robertphillips@google.com | 8f90a89 | 2014-02-28 18:19:39 +0000 | [diff] [blame] | 1578 | this->INHERITED::onClipRRect(rrect, op, edgeStyle); |
| fmalita@google.com | d0f1a4f | 2013-08-27 15:50:19 +0000 | [diff] [blame] | 1579 | } |
| 1580 | |
| skia.committer@gmail.com | 370a899 | 2014-03-01 03:02:09 +0000 | [diff] [blame] | 1581 | virtual void onClipPath(const SkPath& path, |
| 1582 | SkRegion::Op op, |
| robertphillips@google.com | 8f90a89 | 2014-02-28 18:19:39 +0000 | [diff] [blame] | 1583 | ClipEdgeStyle edgeStyle) SK_OVERRIDE { |
| fmalita@google.com | d0f1a4f | 2013-08-27 15:50:19 +0000 | [diff] [blame] | 1584 | fClipCount += 1; |
| robertphillips@google.com | 8f90a89 | 2014-02-28 18:19:39 +0000 | [diff] [blame] | 1585 | this->INHERITED::onClipPath(path, op, edgeStyle); |
| 1586 | } |
| 1587 | |
| 1588 | virtual void onClipRegion(const SkRegion& deviceRgn, SkRegion::Op op) SK_OVERRIDE { |
| 1589 | fClipCount += 1; |
| 1590 | this->INHERITED::onClipRegion(deviceRgn, op); |
| fmalita@google.com | d0f1a4f | 2013-08-27 15:50:19 +0000 | [diff] [blame] | 1591 | } |
| 1592 | |
| 1593 | unsigned getClipCount() const { return fClipCount; } |
| 1594 | |
| 1595 | private: |
| 1596 | unsigned fClipCount; |
| 1597 | |
| 1598 | typedef SkCanvas INHERITED; |
| 1599 | }; |
| 1600 | |
| 1601 | static void test_clip_expansion(skiatest::Reporter* reporter) { |
| robertphillips@google.com | 84b18c7 | 2014-04-13 19:09:42 +0000 | [diff] [blame] | 1602 | SkPictureRecorder recorder; |
| robertphillips | 9f1c241 | 2014-06-09 06:25:34 -0700 | [diff] [blame] | 1603 | SkCanvas* canvas = recorder.beginRecording(10, 10); |
| fmalita@google.com | d0f1a4f | 2013-08-27 15:50:19 +0000 | [diff] [blame] | 1604 | |
| 1605 | canvas->clipRect(SkRect::MakeEmpty(), SkRegion::kReplace_Op); |
| 1606 | // The following expanding clip should not be skipped. |
| 1607 | canvas->clipRect(SkRect::MakeXYWH(4, 4, 3, 3), SkRegion::kUnion_Op); |
| 1608 | // Draw something so the optimizer doesn't just fold the world. |
| 1609 | SkPaint p; |
| 1610 | p.setColor(SK_ColorBLUE); |
| 1611 | canvas->drawPaint(p); |
| robertphillips@google.com | 84b18c7 | 2014-04-13 19:09:42 +0000 | [diff] [blame] | 1612 | SkAutoTUnref<SkPicture> picture(recorder.endRecording()); |
| fmalita@google.com | d0f1a4f | 2013-08-27 15:50:19 +0000 | [diff] [blame] | 1613 | |
| commit-bot@chromium.org | e254310 | 2014-01-31 19:42:58 +0000 | [diff] [blame] | 1614 | ClipCountingCanvas testCanvas(10, 10); |
| robertphillips@google.com | 84b18c7 | 2014-04-13 19:09:42 +0000 | [diff] [blame] | 1615 | picture->draw(&testCanvas); |
| fmalita@google.com | d0f1a4f | 2013-08-27 15:50:19 +0000 | [diff] [blame] | 1616 | |
| 1617 | // Both clips should be present on playback. |
| 1618 | REPORTER_ASSERT(reporter, testCanvas.getClipCount() == 2); |
| 1619 | } |
| 1620 | |
| tomhudson@google.com | 381010e | 2013-10-24 11:12:47 +0000 | [diff] [blame] | 1621 | static void test_hierarchical(skiatest::Reporter* reporter) { |
| 1622 | SkBitmap bm; |
| 1623 | make_bm(&bm, 10, 10, SK_ColorRED, true); |
| 1624 | |
| robertphillips@google.com | 84b18c7 | 2014-04-13 19:09:42 +0000 | [diff] [blame] | 1625 | SkPictureRecorder recorder; |
| tomhudson@google.com | 381010e | 2013-10-24 11:12:47 +0000 | [diff] [blame] | 1626 | |
| robertphillips | 9f1c241 | 2014-06-09 06:25:34 -0700 | [diff] [blame] | 1627 | recorder.beginRecording(10, 10); |
| robertphillips@google.com | 84b18c7 | 2014-04-13 19:09:42 +0000 | [diff] [blame] | 1628 | SkAutoTUnref<SkPicture> childPlain(recorder.endRecording()); |
| 1629 | REPORTER_ASSERT(reporter, !childPlain->willPlayBackBitmaps()); // 0 |
| tomhudson@google.com | 381010e | 2013-10-24 11:12:47 +0000 | [diff] [blame] | 1630 | |
| robertphillips | 9f1c241 | 2014-06-09 06:25:34 -0700 | [diff] [blame] | 1631 | recorder.beginRecording(10, 10)->drawBitmap(bm, 0, 0); |
| robertphillips@google.com | 84b18c7 | 2014-04-13 19:09:42 +0000 | [diff] [blame] | 1632 | SkAutoTUnref<SkPicture> childWithBitmap(recorder.endRecording()); |
| 1633 | REPORTER_ASSERT(reporter, childWithBitmap->willPlayBackBitmaps()); // 1 |
| tomhudson@google.com | 381010e | 2013-10-24 11:12:47 +0000 | [diff] [blame] | 1634 | |
| robertphillips@google.com | 84b18c7 | 2014-04-13 19:09:42 +0000 | [diff] [blame] | 1635 | { |
| robertphillips | 9f1c241 | 2014-06-09 06:25:34 -0700 | [diff] [blame] | 1636 | SkCanvas* canvas = recorder.beginRecording(10, 10); |
| robertphillips | 9b14f26 | 2014-06-04 05:40:44 -0700 | [diff] [blame] | 1637 | canvas->drawPicture(childPlain); |
| robertphillips@google.com | 84b18c7 | 2014-04-13 19:09:42 +0000 | [diff] [blame] | 1638 | SkAutoTUnref<SkPicture> parentPP(recorder.endRecording()); |
| 1639 | REPORTER_ASSERT(reporter, !parentPP->willPlayBackBitmaps()); // 0 |
| 1640 | } |
| 1641 | { |
| robertphillips | 9f1c241 | 2014-06-09 06:25:34 -0700 | [diff] [blame] | 1642 | SkCanvas* canvas = recorder.beginRecording(10, 10); |
| robertphillips | 9b14f26 | 2014-06-04 05:40:44 -0700 | [diff] [blame] | 1643 | canvas->drawPicture(childWithBitmap); |
| robertphillips@google.com | 84b18c7 | 2014-04-13 19:09:42 +0000 | [diff] [blame] | 1644 | SkAutoTUnref<SkPicture> parentPWB(recorder.endRecording()); |
| 1645 | REPORTER_ASSERT(reporter, parentPWB->willPlayBackBitmaps()); // 1 |
| 1646 | } |
| 1647 | { |
| robertphillips | 9f1c241 | 2014-06-09 06:25:34 -0700 | [diff] [blame] | 1648 | SkCanvas* canvas = recorder.beginRecording(10, 10); |
| robertphillips@google.com | 84b18c7 | 2014-04-13 19:09:42 +0000 | [diff] [blame] | 1649 | canvas->drawBitmap(bm, 0, 0); |
| robertphillips | 9b14f26 | 2014-06-04 05:40:44 -0700 | [diff] [blame] | 1650 | canvas->drawPicture(childPlain); |
| robertphillips@google.com | 84b18c7 | 2014-04-13 19:09:42 +0000 | [diff] [blame] | 1651 | SkAutoTUnref<SkPicture> parentWBP(recorder.endRecording()); |
| 1652 | REPORTER_ASSERT(reporter, parentWBP->willPlayBackBitmaps()); // 1 |
| 1653 | } |
| 1654 | { |
| robertphillips | 9f1c241 | 2014-06-09 06:25:34 -0700 | [diff] [blame] | 1655 | SkCanvas* canvas = recorder.beginRecording(10, 10); |
| robertphillips@google.com | 84b18c7 | 2014-04-13 19:09:42 +0000 | [diff] [blame] | 1656 | canvas->drawBitmap(bm, 0, 0); |
| robertphillips | 9b14f26 | 2014-06-04 05:40:44 -0700 | [diff] [blame] | 1657 | canvas->drawPicture(childWithBitmap); |
| robertphillips@google.com | 84b18c7 | 2014-04-13 19:09:42 +0000 | [diff] [blame] | 1658 | SkAutoTUnref<SkPicture> parentWBWB(recorder.endRecording()); |
| 1659 | REPORTER_ASSERT(reporter, parentWBWB->willPlayBackBitmaps()); // 2 |
| 1660 | } |
| tomhudson@google.com | 381010e | 2013-10-24 11:12:47 +0000 | [diff] [blame] | 1661 | } |
| 1662 | |
| robertphillips@google.com | d550088 | 2014-04-02 23:51:13 +0000 | [diff] [blame] | 1663 | static void test_gen_id(skiatest::Reporter* reporter) { |
| 1664 | |
| Robert Phillips | cfaeec4 | 2014-07-13 12:00:50 -0400 | [diff] [blame] | 1665 | SkPictureRecorder recorder; |
| 1666 | recorder.beginRecording(0, 0); |
| 1667 | SkAutoTUnref<SkPicture> empty(recorder.endRecording()); |
| robertphillips@google.com | d550088 | 2014-04-02 23:51:13 +0000 | [diff] [blame] | 1668 | |
| robertphillips@google.com | 84b18c7 | 2014-04-13 19:09:42 +0000 | [diff] [blame] | 1669 | // Empty pictures should still have a valid ID |
| Robert Phillips | cfaeec4 | 2014-07-13 12:00:50 -0400 | [diff] [blame] | 1670 | REPORTER_ASSERT(reporter, empty->uniqueID() != SK_InvalidGenID); |
| robertphillips@google.com | d550088 | 2014-04-02 23:51:13 +0000 | [diff] [blame] | 1671 | |
| robertphillips | 9f1c241 | 2014-06-09 06:25:34 -0700 | [diff] [blame] | 1672 | SkCanvas* canvas = recorder.beginRecording(1, 1); |
| robertphillips@google.com | 84b18c7 | 2014-04-13 19:09:42 +0000 | [diff] [blame] | 1673 | canvas->drawARGB(255, 255, 255, 255); |
| 1674 | SkAutoTUnref<SkPicture> hasData(recorder.endRecording()); |
| 1675 | // picture should have a non-zero id after recording |
| 1676 | REPORTER_ASSERT(reporter, hasData->uniqueID() != SK_InvalidGenID); |
| robertphillips@google.com | d550088 | 2014-04-02 23:51:13 +0000 | [diff] [blame] | 1677 | |
| robertphillips@google.com | 84b18c7 | 2014-04-13 19:09:42 +0000 | [diff] [blame] | 1678 | // both pictures should have different ids |
| Robert Phillips | cfaeec4 | 2014-07-13 12:00:50 -0400 | [diff] [blame] | 1679 | REPORTER_ASSERT(reporter, hasData->uniqueID() != empty->uniqueID()); |
| robertphillips@google.com | d550088 | 2014-04-02 23:51:13 +0000 | [diff] [blame] | 1680 | } |
| 1681 | |
| tfarina@chromium.org | e4fafb1 | 2013-12-12 21:11:12 +0000 | [diff] [blame] | 1682 | DEF_TEST(Picture, reporter) { |
| scroggo@google.com | d614c6a | 2012-09-14 17:26:37 +0000 | [diff] [blame] | 1683 | #ifdef SK_DEBUG |
| robertphillips | db53990 | 2014-07-01 08:47:04 -0700 | [diff] [blame] | 1684 | test_deleting_empty_picture(); |
| scroggo@google.com | d614c6a | 2012-09-14 17:26:37 +0000 | [diff] [blame] | 1685 | test_serializing_empty_picture(); |
| scroggo@google.com | 4b90b11 | 2012-12-04 15:08:56 +0000 | [diff] [blame] | 1686 | #else |
| 1687 | test_bad_bitmap(); |
| scroggo@google.com | d614c6a | 2012-09-14 17:26:37 +0000 | [diff] [blame] | 1688 | #endif |
| commit-bot@chromium.org | ea7d08e | 2014-02-13 16:00:51 +0000 | [diff] [blame] | 1689 | test_unbalanced_save_restores(reporter); |
| sugoi@google.com | 54f0d1b | 2013-02-27 19:17:41 +0000 | [diff] [blame] | 1690 | test_peephole(); |
| robertphillips@google.com | b950c6f | 2014-04-25 00:02:12 +0000 | [diff] [blame] | 1691 | #if SK_SUPPORT_GPU |
| tomhudson | 60c2a79 | 2014-08-18 15:07:13 -0700 | [diff] [blame] | 1692 | test_gpu_veto(reporter, false); |
| 1693 | test_gpu_veto(reporter, true); |
| robertphillips@google.com | b950c6f | 2014-04-25 00:02:12 +0000 | [diff] [blame] | 1694 | #endif |
| ajuma | 750ae26 | 2014-08-18 12:59:55 -0700 | [diff] [blame] | 1695 | test_has_text(reporter); |
| tomhudson | 60c2a79 | 2014-08-18 15:07:13 -0700 | [diff] [blame] | 1696 | test_analysis(reporter, false); |
| 1697 | test_analysis(reporter, true); |
| reed@google.com | fe7b1ed | 2012-11-29 21:00:39 +0000 | [diff] [blame] | 1698 | test_gatherpixelrefs(reporter); |
| robertphillips@google.com | 56bf6e4 | 2014-01-13 13:33:26 +0000 | [diff] [blame] | 1699 | test_gatherpixelrefsandrects(reporter); |
| scroggo@google.com | 7c9d539 | 2012-12-10 15:40:55 +0000 | [diff] [blame] | 1700 | test_bitmap_with_encoded_data(reporter); |
| commit-bot@chromium.org | 70512af | 2014-03-18 17:45:32 +0000 | [diff] [blame] | 1701 | test_draw_empty(reporter); |
| junov@chromium.org | d575eed | 2013-05-08 15:39:13 +0000 | [diff] [blame] | 1702 | test_clip_bound_opt(reporter); |
| fmalita@google.com | d0f1a4f | 2013-08-27 15:50:19 +0000 | [diff] [blame] | 1703 | test_clip_expansion(reporter); |
| tomhudson@google.com | 381010e | 2013-10-24 11:12:47 +0000 | [diff] [blame] | 1704 | test_hierarchical(reporter); |
| robertphillips@google.com | d550088 | 2014-04-02 23:51:13 +0000 | [diff] [blame] | 1705 | test_gen_id(reporter); |
| scroggo@google.com | d614c6a | 2012-09-14 17:26:37 +0000 | [diff] [blame] | 1706 | } |
| commit-bot@chromium.org | 50b393a | 2014-02-10 18:29:10 +0000 | [diff] [blame] | 1707 | |
| commit-bot@chromium.org | 0205aba | 2014-05-06 12:02:22 +0000 | [diff] [blame] | 1708 | #if SK_SUPPORT_GPU |
| 1709 | DEF_GPUTEST(GPUPicture, reporter, factory) { |
| 1710 | test_gpu_picture_optimization(reporter, factory); |
| 1711 | } |
| 1712 | #endif |
| 1713 | |
| commit-bot@chromium.org | 50b393a | 2014-02-10 18:29:10 +0000 | [diff] [blame] | 1714 | static void draw_bitmaps(const SkBitmap bitmap, SkCanvas* canvas) { |
| 1715 | const SkPaint paint; |
| 1716 | const SkRect rect = { 5.0f, 5.0f, 8.0f, 8.0f }; |
| 1717 | const SkIRect irect = { 2, 2, 3, 3 }; |
| 1718 | |
| 1719 | // Don't care what these record, as long as they're legal. |
| 1720 | canvas->drawBitmap(bitmap, 0.0f, 0.0f, &paint); |
| 1721 | canvas->drawBitmapRectToRect(bitmap, &rect, rect, &paint, SkCanvas::kNone_DrawBitmapRectFlag); |
| 1722 | canvas->drawBitmapMatrix(bitmap, SkMatrix::I(), &paint); |
| 1723 | canvas->drawBitmapNine(bitmap, irect, rect, &paint); |
| 1724 | canvas->drawSprite(bitmap, 1, 1); |
| 1725 | } |
| 1726 | |
| 1727 | static void test_draw_bitmaps(SkCanvas* canvas) { |
| 1728 | SkBitmap empty; |
| 1729 | draw_bitmaps(empty, canvas); |
| commit-bot@chromium.org | a3264e5 | 2014-05-30 13:26:10 +0000 | [diff] [blame] | 1730 | empty.setInfo(SkImageInfo::MakeN32Premul(10, 10)); |
| commit-bot@chromium.org | 50b393a | 2014-02-10 18:29:10 +0000 | [diff] [blame] | 1731 | draw_bitmaps(empty, canvas); |
| 1732 | } |
| 1733 | |
| 1734 | DEF_TEST(Picture_EmptyBitmap, r) { |
| robertphillips@google.com | 84b18c7 | 2014-04-13 19:09:42 +0000 | [diff] [blame] | 1735 | SkPictureRecorder recorder; |
| robertphillips | 9f1c241 | 2014-06-09 06:25:34 -0700 | [diff] [blame] | 1736 | test_draw_bitmaps(recorder.beginRecording(10, 10)); |
| robertphillips@google.com | 84b18c7 | 2014-04-13 19:09:42 +0000 | [diff] [blame] | 1737 | SkAutoTUnref<SkPicture> picture(recorder.endRecording()); |
| commit-bot@chromium.org | 50b393a | 2014-02-10 18:29:10 +0000 | [diff] [blame] | 1738 | } |
| 1739 | |
| 1740 | DEF_TEST(Canvas_EmptyBitmap, r) { |
| 1741 | SkBitmap dst; |
| mike@reedtribe.org | deee496 | 2014-02-13 14:41:43 +0000 | [diff] [blame] | 1742 | dst.allocN32Pixels(10, 10); |
| commit-bot@chromium.org | 50b393a | 2014-02-10 18:29:10 +0000 | [diff] [blame] | 1743 | SkCanvas canvas(dst); |
| 1744 | |
| 1745 | test_draw_bitmaps(&canvas); |
| 1746 | } |
| dneto | 3f22e8c | 2014-07-30 15:42:22 -0700 | [diff] [blame] | 1747 | |
| 1748 | DEF_TEST(DontOptimizeSaveLayerDrawDrawRestore, reporter) { |
| 1749 | // This test is from crbug.com/344987. |
| 1750 | // The commands are: |
| 1751 | // saveLayer with paint that modifies alpha |
| 1752 | // drawBitmapRectToRect |
| 1753 | // drawBitmapRectToRect |
| 1754 | // restore |
| 1755 | // The bug was that this structure was modified so that: |
| 1756 | // - The saveLayer and restore were eliminated |
| 1757 | // - The alpha was only applied to the first drawBitmapRectToRect |
| 1758 | |
| 1759 | // This test draws blue and red squares inside a 50% transparent |
| 1760 | // layer. Both colours should show up muted. |
| 1761 | // When the bug is present, the red square (the second bitmap) |
| 1762 | // shows upwith full opacity. |
| 1763 | |
| 1764 | SkBitmap blueBM; |
| 1765 | make_bm(&blueBM, 100, 100, SkColorSetARGB(255, 0, 0, 255), true); |
| 1766 | SkBitmap redBM; |
| 1767 | make_bm(&redBM, 100, 100, SkColorSetARGB(255, 255, 0, 0), true); |
| 1768 | SkPaint semiTransparent; |
| 1769 | semiTransparent.setAlpha(0x80); |
| 1770 | |
| 1771 | SkPictureRecorder recorder; |
| 1772 | SkCanvas* canvas = recorder.beginRecording(100, 100); |
| 1773 | canvas->drawARGB(0, 0, 0, 0); |
| 1774 | |
| 1775 | canvas->saveLayer(0, &semiTransparent); |
| 1776 | canvas->drawBitmap(blueBM, 25, 25); |
| 1777 | canvas->drawBitmap(redBM, 50, 50); |
| 1778 | canvas->restore(); |
| 1779 | |
| 1780 | SkAutoTUnref<SkPicture> picture(recorder.endRecording()); |
| 1781 | |
| 1782 | // Now replay the picture back on another canvas |
| 1783 | // and check a couple of its pixels. |
| 1784 | SkBitmap replayBM; |
| 1785 | make_bm(&replayBM, 100, 100, SK_ColorBLACK, false); |
| 1786 | SkCanvas replayCanvas(replayBM); |
| 1787 | picture->draw(&replayCanvas); |
| 1788 | replayCanvas.flush(); |
| 1789 | |
| 1790 | // With the bug present, at (55, 55) we would get a fully opaque red |
| 1791 | // intead of a dark red. |
| 1792 | REPORTER_ASSERT(reporter, replayBM.getColor(30, 30) == 0xff000080); |
| 1793 | REPORTER_ASSERT(reporter, replayBM.getColor(55, 55) == 0xff800000); |
| 1794 | } |
| mtklein | 3e8232b | 2014-08-18 13:39:11 -0700 | [diff] [blame] | 1795 | |
| 1796 | struct CountingBBH : public SkBBoxHierarchy { |
| 1797 | mutable int searchCalls; |
| 1798 | |
| 1799 | CountingBBH() : searchCalls(0) {} |
| 1800 | |
| 1801 | virtual void search(const SkIRect& query, SkTDArray<void*>* results) const { |
| 1802 | this->searchCalls++; |
| 1803 | } |
| 1804 | |
| 1805 | // All other methods unimplemented. |
| 1806 | virtual void insert(void* data, const SkIRect& bounds, bool defer) {} |
| 1807 | virtual void flushDeferredInserts() {} |
| 1808 | virtual void clear() {} |
| 1809 | virtual int getCount() const { return 0; } |
| 1810 | virtual int getDepth() const { return 0; } |
| 1811 | virtual void rewindInserts() {} |
| 1812 | }; |
| 1813 | |
| 1814 | class SpoonFedBBHFactory : public SkBBHFactory { |
| 1815 | public: |
| 1816 | explicit SpoonFedBBHFactory(SkBBoxHierarchy* bbh) : fBBH(bbh) {} |
| 1817 | virtual SkBBoxHierarchy* operator()(int width, int height) const { |
| 1818 | return SkRef(fBBH); |
| 1819 | } |
| 1820 | private: |
| 1821 | SkBBoxHierarchy* fBBH; |
| 1822 | }; |
| 1823 | |
| 1824 | // When the canvas clip covers the full picture, we don't need to call the BBH. |
| 1825 | DEF_TEST(Picture_SkipBBH, r) { |
| 1826 | CountingBBH bbh; |
| 1827 | SpoonFedBBHFactory factory(&bbh); |
| 1828 | |
| 1829 | SkPictureRecorder recorder; |
| 1830 | recorder.beginRecording(320, 240, &factory); |
| 1831 | SkAutoTUnref<const SkPicture> picture(recorder.endRecording()); |
| 1832 | |
| 1833 | SkCanvas big(640, 480), small(300, 200); |
| 1834 | |
| 1835 | picture->draw(&big); |
| 1836 | REPORTER_ASSERT(r, bbh.searchCalls == 0); |
| 1837 | |
| 1838 | picture->draw(&small); |
| 1839 | REPORTER_ASSERT(r, bbh.searchCalls == 1); |
| 1840 | } |