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 | |
scroggo@google.com | d614c6a | 2012-09-14 17:26:37 +0000 | [diff] [blame] | 8 | #include "Test.h" |
tfarina@chromium.org | e4fafb1 | 2013-12-12 21:11:12 +0000 | [diff] [blame] | 9 | #include "TestClassDef.h" |
robertphillips@google.com | 1f2f338 | 2013-08-29 11:54:56 +0000 | [diff] [blame] | 10 | #include "SkBitmapDevice.h" |
reed@google.com | 21b519d | 2012-10-02 17:42:15 +0000 | [diff] [blame] | 11 | #include "SkCanvas.h" |
reed@google.com | fe7b1ed | 2012-11-29 21:00:39 +0000 | [diff] [blame] | 12 | #include "SkColorPriv.h" |
| 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 | 1f2f338 | 2013-08-29 11:54:56 +0000 | [diff] [blame] | 20 | #include "SkPictureUtils.h" |
reed@google.com | 21b519d | 2012-10-02 17:42:15 +0000 | [diff] [blame] | 21 | #include "SkRandom.h" |
reed@google.com | 72aa79c | 2013-01-24 18:27:42 +0000 | [diff] [blame] | 22 | #include "SkRRect.h" |
reed@google.com | fe7b1ed | 2012-11-29 21:00:39 +0000 | [diff] [blame] | 23 | #include "SkShader.h" |
scroggo@google.com | d614c6a | 2012-09-14 17:26:37 +0000 | [diff] [blame] | 24 | #include "SkStream.h" |
| 25 | |
robertphillips@google.com | ed9866c | 2014-01-09 19:20:45 +0000 | [diff] [blame] | 26 | static const int gColorScale = 30; |
| 27 | static const int gColorOffset = 60; |
reed@google.com | fe7b1ed | 2012-11-29 21:00:39 +0000 | [diff] [blame] | 28 | |
| 29 | static void make_bm(SkBitmap* bm, int w, int h, SkColor color, bool immutable) { |
| 30 | bm->setConfig(SkBitmap::kARGB_8888_Config, w, h); |
| 31 | bm->allocPixels(); |
| 32 | bm->eraseColor(color); |
| 33 | if (immutable) { |
| 34 | bm->setImmutable(); |
| 35 | } |
| 36 | } |
| 37 | |
robertphillips@google.com | fe5824a | 2014-01-09 19:45:29 +0000 | [diff] [blame] | 38 | 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] | 39 | SkASSERT(w % 2 == 0); |
| 40 | SkASSERT(h % 2 == 0); |
| 41 | bm->setConfig(SkBitmap::kA8_Config, w, h); |
| 42 | bm->allocPixels(); |
| 43 | SkAutoLockPixels lock(*bm); |
| 44 | for (int y = 0; y < h; y += 2) { |
| 45 | uint8_t* s = bm->getAddr8(0, y); |
| 46 | for (int x = 0; x < w; x += 2) { |
| 47 | *s++ = 0xFF; |
| 48 | *s++ = 0x00; |
| 49 | } |
| 50 | s = bm->getAddr8(0, y + 1); |
| 51 | for (int x = 0; x < w; x += 2) { |
| 52 | *s++ = 0x00; |
| 53 | *s++ = 0xFF; |
| 54 | } |
| 55 | } |
| 56 | if (immutable) { |
| 57 | bm->setImmutable(); |
| 58 | } |
| 59 | } |
reed@google.com | fe7b1ed | 2012-11-29 21:00:39 +0000 | [diff] [blame] | 60 | |
robertphillips@google.com | ed9866c | 2014-01-09 19:20:45 +0000 | [diff] [blame] | 61 | static void init_paint(SkPaint* paint, const SkBitmap &bm) { |
| 62 | SkShader* shader = SkShader::CreateBitmapShader(bm, |
| 63 | SkShader::kClamp_TileMode, |
| 64 | SkShader::kClamp_TileMode); |
| 65 | paint->setShader(shader)->unref(); |
| 66 | } |
| 67 | |
skia.committer@gmail.com | 2e9a715 | 2014-01-14 07:01:42 +0000 | [diff] [blame] | 68 | typedef void (*DrawBitmapProc)(SkCanvas*, const SkBitmap&, |
robertphillips@google.com | 56bf6e4 | 2014-01-13 13:33:26 +0000 | [diff] [blame] | 69 | const SkBitmap&, const SkPoint&, |
| 70 | SkTDArray<SkPixelRef*>* usedPixRefs); |
robertphillips@google.com | ed9866c | 2014-01-09 19:20:45 +0000 | [diff] [blame] | 71 | |
skia.committer@gmail.com | 856673a | 2014-01-10 07:08:11 +0000 | [diff] [blame] | 72 | static void drawpaint_proc(SkCanvas* canvas, const SkBitmap& bm, |
robertphillips@google.com | 56bf6e4 | 2014-01-13 13:33:26 +0000 | [diff] [blame] | 73 | const SkBitmap& altBM, const SkPoint& pos, |
| 74 | SkTDArray<SkPixelRef*>* usedPixRefs) { |
robertphillips@google.com | ed9866c | 2014-01-09 19:20:45 +0000 | [diff] [blame] | 75 | SkPaint paint; |
| 76 | init_paint(&paint, bm); |
| 77 | |
| 78 | canvas->drawPaint(paint); |
robertphillips@google.com | 56bf6e4 | 2014-01-13 13:33:26 +0000 | [diff] [blame] | 79 | *usedPixRefs->append() = bm.pixelRef(); |
robertphillips@google.com | ed9866c | 2014-01-09 19:20:45 +0000 | [diff] [blame] | 80 | } |
| 81 | |
skia.committer@gmail.com | 856673a | 2014-01-10 07:08:11 +0000 | [diff] [blame] | 82 | static void drawpoints_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 | |
robertphillips@google.com | 56bf6e4 | 2014-01-13 13:33:26 +0000 | [diff] [blame] | 88 | // draw a rect |
robertphillips@google.com | ed9866c | 2014-01-09 19:20:45 +0000 | [diff] [blame] | 89 | SkPoint points[5] = { |
skia.committer@gmail.com | 2e9a715 | 2014-01-14 07:01:42 +0000 | [diff] [blame] | 90 | { pos.fX, pos.fY }, |
| 91 | { pos.fX + bm.width() - 1, pos.fY }, |
| 92 | { pos.fX + bm.width() - 1, pos.fY + bm.height() - 1 }, |
| 93 | { pos.fX, pos.fY + bm.height() - 1 }, |
| 94 | { pos.fX, pos.fY }, |
robertphillips@google.com | ed9866c | 2014-01-09 19:20:45 +0000 | [diff] [blame] | 95 | }; |
| 96 | |
| 97 | canvas->drawPoints(SkCanvas::kPolygon_PointMode, 5, points, paint); |
robertphillips@google.com | 56bf6e4 | 2014-01-13 13:33:26 +0000 | [diff] [blame] | 98 | *usedPixRefs->append() = bm.pixelRef(); |
robertphillips@google.com | ed9866c | 2014-01-09 19:20:45 +0000 | [diff] [blame] | 99 | } |
| 100 | |
skia.committer@gmail.com | 856673a | 2014-01-10 07:08:11 +0000 | [diff] [blame] | 101 | static void drawrect_proc(SkCanvas* canvas, const SkBitmap& bm, |
robertphillips@google.com | 56bf6e4 | 2014-01-13 13:33:26 +0000 | [diff] [blame] | 102 | const SkBitmap& altBM, const SkPoint& pos, |
| 103 | SkTDArray<SkPixelRef*>* usedPixRefs) { |
robertphillips@google.com | ed9866c | 2014-01-09 19:20:45 +0000 | [diff] [blame] | 104 | SkPaint paint; |
| 105 | init_paint(&paint, bm); |
| 106 | |
| 107 | SkRect r = { 0, 0, SkIntToScalar(bm.width()), SkIntToScalar(bm.height()) }; |
| 108 | r.offset(pos.fX, pos.fY); |
| 109 | |
| 110 | canvas->drawRect(r, paint); |
robertphillips@google.com | 56bf6e4 | 2014-01-13 13:33:26 +0000 | [diff] [blame] | 111 | *usedPixRefs->append() = bm.pixelRef(); |
robertphillips@google.com | ed9866c | 2014-01-09 19:20:45 +0000 | [diff] [blame] | 112 | } |
| 113 | |
skia.committer@gmail.com | 856673a | 2014-01-10 07:08:11 +0000 | [diff] [blame] | 114 | static void drawoval_proc(SkCanvas* canvas, const SkBitmap& bm, |
robertphillips@google.com | 56bf6e4 | 2014-01-13 13:33:26 +0000 | [diff] [blame] | 115 | const SkBitmap& altBM, const SkPoint& pos, |
| 116 | SkTDArray<SkPixelRef*>* usedPixRefs) { |
robertphillips@google.com | ed9866c | 2014-01-09 19:20:45 +0000 | [diff] [blame] | 117 | SkPaint paint; |
| 118 | init_paint(&paint, bm); |
| 119 | |
| 120 | SkRect r = { 0, 0, SkIntToScalar(bm.width()), SkIntToScalar(bm.height()) }; |
| 121 | r.offset(pos.fX, pos.fY); |
| 122 | |
| 123 | canvas->drawOval(r, paint); |
robertphillips@google.com | 56bf6e4 | 2014-01-13 13:33:26 +0000 | [diff] [blame] | 124 | *usedPixRefs->append() = bm.pixelRef(); |
robertphillips@google.com | ed9866c | 2014-01-09 19:20:45 +0000 | [diff] [blame] | 125 | } |
| 126 | |
skia.committer@gmail.com | 856673a | 2014-01-10 07:08:11 +0000 | [diff] [blame] | 127 | static void drawrrect_proc(SkCanvas* canvas, const SkBitmap& bm, |
robertphillips@google.com | 56bf6e4 | 2014-01-13 13:33:26 +0000 | [diff] [blame] | 128 | const SkBitmap& altBM, const SkPoint& pos, |
| 129 | SkTDArray<SkPixelRef*>* usedPixRefs) { |
robertphillips@google.com | ed9866c | 2014-01-09 19:20:45 +0000 | [diff] [blame] | 130 | SkPaint paint; |
| 131 | init_paint(&paint, bm); |
skia.committer@gmail.com | 856673a | 2014-01-10 07:08:11 +0000 | [diff] [blame] | 132 | |
robertphillips@google.com | ed9866c | 2014-01-09 19:20:45 +0000 | [diff] [blame] | 133 | SkRect r = { 0, 0, SkIntToScalar(bm.width()), SkIntToScalar(bm.height()) }; |
| 134 | r.offset(pos.fX, pos.fY); |
| 135 | |
| 136 | SkRRect rr; |
| 137 | rr.setRectXY(r, SkIntToScalar(bm.width())/4, SkIntToScalar(bm.height())/4); |
| 138 | canvas->drawRRect(rr, paint); |
robertphillips@google.com | 56bf6e4 | 2014-01-13 13:33:26 +0000 | [diff] [blame] | 139 | *usedPixRefs->append() = bm.pixelRef(); |
robertphillips@google.com | ed9866c | 2014-01-09 19:20:45 +0000 | [diff] [blame] | 140 | } |
| 141 | |
skia.committer@gmail.com | 856673a | 2014-01-10 07:08:11 +0000 | [diff] [blame] | 142 | static void drawpath_proc(SkCanvas* canvas, const SkBitmap& bm, |
robertphillips@google.com | 56bf6e4 | 2014-01-13 13:33:26 +0000 | [diff] [blame] | 143 | const SkBitmap& altBM, const SkPoint& pos, |
| 144 | SkTDArray<SkPixelRef*>* usedPixRefs) { |
robertphillips@google.com | ed9866c | 2014-01-09 19:20:45 +0000 | [diff] [blame] | 145 | SkPaint paint; |
| 146 | init_paint(&paint, bm); |
| 147 | |
| 148 | SkPath path; |
| 149 | path.lineTo(bm.width()/2.0f, SkIntToScalar(bm.height())); |
| 150 | path.lineTo(SkIntToScalar(bm.width()), 0); |
| 151 | path.close(); |
| 152 | path.offset(pos.fX, pos.fY); |
| 153 | |
| 154 | canvas->drawPath(path, paint); |
robertphillips@google.com | 56bf6e4 | 2014-01-13 13:33:26 +0000 | [diff] [blame] | 155 | *usedPixRefs->append() = bm.pixelRef(); |
robertphillips@google.com | ed9866c | 2014-01-09 19:20:45 +0000 | [diff] [blame] | 156 | } |
| 157 | |
skia.committer@gmail.com | 856673a | 2014-01-10 07:08:11 +0000 | [diff] [blame] | 158 | static void drawbitmap_proc(SkCanvas* canvas, const SkBitmap& bm, |
robertphillips@google.com | 56bf6e4 | 2014-01-13 13:33:26 +0000 | [diff] [blame] | 159 | const SkBitmap& altBM, const SkPoint& pos, |
| 160 | SkTDArray<SkPixelRef*>* usedPixRefs) { |
reed@google.com | fe7b1ed | 2012-11-29 21:00:39 +0000 | [diff] [blame] | 161 | canvas->drawBitmap(bm, pos.fX, pos.fY, NULL); |
robertphillips@google.com | 56bf6e4 | 2014-01-13 13:33:26 +0000 | [diff] [blame] | 162 | *usedPixRefs->append() = bm.pixelRef(); |
reed@google.com | fe7b1ed | 2012-11-29 21:00:39 +0000 | [diff] [blame] | 163 | } |
| 164 | |
skia.committer@gmail.com | 856673a | 2014-01-10 07:08:11 +0000 | [diff] [blame] | 165 | static void drawbitmap_withshader_proc(SkCanvas* canvas, const SkBitmap& bm, |
robertphillips@google.com | 56bf6e4 | 2014-01-13 13:33:26 +0000 | [diff] [blame] | 166 | const SkBitmap& altBM, const SkPoint& pos, |
| 167 | SkTDArray<SkPixelRef*>* usedPixRefs) { |
robertphillips@google.com | ed9866c | 2014-01-09 19:20:45 +0000 | [diff] [blame] | 168 | SkPaint paint; |
| 169 | init_paint(&paint, bm); |
| 170 | |
| 171 | // The bitmap in the paint is ignored unless we're drawing an A8 bitmap |
| 172 | canvas->drawBitmap(altBM, pos.fX, pos.fY, &paint); |
robertphillips@google.com | 56bf6e4 | 2014-01-13 13:33:26 +0000 | [diff] [blame] | 173 | *usedPixRefs->append() = bm.pixelRef(); |
| 174 | *usedPixRefs->append() = altBM.pixelRef(); |
robertphillips@google.com | ed9866c | 2014-01-09 19:20:45 +0000 | [diff] [blame] | 175 | } |
| 176 | |
skia.committer@gmail.com | 856673a | 2014-01-10 07:08:11 +0000 | [diff] [blame] | 177 | static void drawsprite_proc(SkCanvas* canvas, const SkBitmap& bm, |
robertphillips@google.com | 56bf6e4 | 2014-01-13 13:33:26 +0000 | [diff] [blame] | 178 | const SkBitmap& altBM, const SkPoint& pos, |
| 179 | SkTDArray<SkPixelRef*>* usedPixRefs) { |
robertphillips@google.com | ed9866c | 2014-01-09 19:20:45 +0000 | [diff] [blame] | 180 | const SkMatrix& ctm = canvas->getTotalMatrix(); |
| 181 | |
| 182 | SkPoint p(pos); |
| 183 | ctm.mapPoints(&p, 1); |
| 184 | |
| 185 | canvas->drawSprite(bm, (int)p.fX, (int)p.fY, NULL); |
robertphillips@google.com | 56bf6e4 | 2014-01-13 13:33:26 +0000 | [diff] [blame] | 186 | *usedPixRefs->append() = bm.pixelRef(); |
robertphillips@google.com | ed9866c | 2014-01-09 19:20:45 +0000 | [diff] [blame] | 187 | } |
| 188 | |
| 189 | #if 0 |
| 190 | // Although specifiable, this case doesn't seem to make sense (i.e., the |
| 191 | // bitmap in the shader is never used). |
skia.committer@gmail.com | 856673a | 2014-01-10 07:08:11 +0000 | [diff] [blame] | 192 | static void drawsprite_withshader_proc(SkCanvas* canvas, const SkBitmap& bm, |
robertphillips@google.com | 56bf6e4 | 2014-01-13 13:33:26 +0000 | [diff] [blame] | 193 | const SkBitmap& altBM, const SkPoint& pos, |
| 194 | SkTDArray<SkPixelRef*>* usedPixRefs) { |
robertphillips@google.com | ed9866c | 2014-01-09 19:20:45 +0000 | [diff] [blame] | 195 | SkPaint paint; |
| 196 | init_paint(&paint, bm); |
| 197 | |
| 198 | const SkMatrix& ctm = canvas->getTotalMatrix(); |
| 199 | |
| 200 | SkPoint p(pos); |
| 201 | ctm.mapPoints(&p, 1); |
| 202 | |
| 203 | canvas->drawSprite(altBM, (int)p.fX, (int)p.fY, &paint); |
robertphillips@google.com | 56bf6e4 | 2014-01-13 13:33:26 +0000 | [diff] [blame] | 204 | *usedPixRefs->append() = bm.pixelRef(); |
| 205 | *usedPixRefs->append() = altBM.pixelRef(); |
robertphillips@google.com | ed9866c | 2014-01-09 19:20:45 +0000 | [diff] [blame] | 206 | } |
| 207 | #endif |
| 208 | |
skia.committer@gmail.com | 856673a | 2014-01-10 07:08:11 +0000 | [diff] [blame] | 209 | static void drawbitmaprect_proc(SkCanvas* canvas, const SkBitmap& bm, |
robertphillips@google.com | 56bf6e4 | 2014-01-13 13:33:26 +0000 | [diff] [blame] | 210 | const SkBitmap& altBM, const SkPoint& pos, |
| 211 | SkTDArray<SkPixelRef*>* usedPixRefs) { |
robertphillips@google.com | ed9866c | 2014-01-09 19:20:45 +0000 | [diff] [blame] | 212 | SkRect r = { 0, 0, SkIntToScalar(bm.width()), SkIntToScalar(bm.height()) }; |
| 213 | |
reed@google.com | fe7b1ed | 2012-11-29 21:00:39 +0000 | [diff] [blame] | 214 | r.offset(pos.fX, pos.fY); |
| 215 | canvas->drawBitmapRectToRect(bm, NULL, r, NULL); |
robertphillips@google.com | 56bf6e4 | 2014-01-13 13:33:26 +0000 | [diff] [blame] | 216 | *usedPixRefs->append() = bm.pixelRef(); |
reed@google.com | fe7b1ed | 2012-11-29 21:00:39 +0000 | [diff] [blame] | 217 | } |
| 218 | |
skia.committer@gmail.com | 856673a | 2014-01-10 07:08:11 +0000 | [diff] [blame] | 219 | static void drawbitmaprect_withshader_proc(SkCanvas* canvas, |
| 220 | const SkBitmap& bm, |
| 221 | const SkBitmap& altBM, |
robertphillips@google.com | 56bf6e4 | 2014-01-13 13:33:26 +0000 | [diff] [blame] | 222 | const SkPoint& pos, |
| 223 | SkTDArray<SkPixelRef*>* usedPixRefs) { |
robertphillips@google.com | ed9866c | 2014-01-09 19:20:45 +0000 | [diff] [blame] | 224 | SkPaint paint; |
| 225 | init_paint(&paint, bm); |
| 226 | |
| 227 | SkRect r = { 0, 0, SkIntToScalar(bm.width()), SkIntToScalar(bm.height()) }; |
reed@google.com | fe7b1ed | 2012-11-29 21:00:39 +0000 | [diff] [blame] | 228 | r.offset(pos.fX, pos.fY); |
| 229 | |
robertphillips@google.com | ed9866c | 2014-01-09 19:20:45 +0000 | [diff] [blame] | 230 | // The bitmap in the paint is ignored unless we're drawing an A8 bitmap |
| 231 | canvas->drawBitmapRectToRect(altBM, NULL, r, &paint); |
robertphillips@google.com | 56bf6e4 | 2014-01-13 13:33:26 +0000 | [diff] [blame] | 232 | *usedPixRefs->append() = bm.pixelRef(); |
| 233 | *usedPixRefs->append() = altBM.pixelRef(); |
robertphillips@google.com | ed9866c | 2014-01-09 19:20:45 +0000 | [diff] [blame] | 234 | } |
| 235 | |
skia.committer@gmail.com | 856673a | 2014-01-10 07:08:11 +0000 | [diff] [blame] | 236 | static void drawtext_proc(SkCanvas* canvas, const SkBitmap& bm, |
robertphillips@google.com | 56bf6e4 | 2014-01-13 13:33:26 +0000 | [diff] [blame] | 237 | const SkBitmap& altBM, const SkPoint& pos, |
| 238 | SkTDArray<SkPixelRef*>* usedPixRefs) { |
reed@google.com | fe7b1ed | 2012-11-29 21:00:39 +0000 | [diff] [blame] | 239 | SkPaint paint; |
robertphillips@google.com | ed9866c | 2014-01-09 19:20:45 +0000 | [diff] [blame] | 240 | init_paint(&paint, bm); |
| 241 | paint.setTextSize(SkIntToScalar(1.5*bm.width())); |
| 242 | |
| 243 | canvas->drawText("0", 1, pos.fX, pos.fY+bm.width(), paint); |
robertphillips@google.com | 56bf6e4 | 2014-01-13 13:33:26 +0000 | [diff] [blame] | 244 | *usedPixRefs->append() = bm.pixelRef(); |
robertphillips@google.com | ed9866c | 2014-01-09 19:20:45 +0000 | [diff] [blame] | 245 | } |
| 246 | |
skia.committer@gmail.com | 856673a | 2014-01-10 07:08:11 +0000 | [diff] [blame] | 247 | static void drawpostext_proc(SkCanvas* canvas, const SkBitmap& bm, |
robertphillips@google.com | 56bf6e4 | 2014-01-13 13:33:26 +0000 | [diff] [blame] | 248 | const SkBitmap& altBM, const SkPoint& pos, |
| 249 | SkTDArray<SkPixelRef*>* usedPixRefs) { |
robertphillips@google.com | ed9866c | 2014-01-09 19:20:45 +0000 | [diff] [blame] | 250 | SkPaint paint; |
| 251 | init_paint(&paint, bm); |
| 252 | paint.setTextSize(SkIntToScalar(1.5*bm.width())); |
| 253 | |
| 254 | SkPoint point = { pos.fX, pos.fY + bm.height() }; |
| 255 | canvas->drawPosText("O", 1, &point, paint); |
robertphillips@google.com | 56bf6e4 | 2014-01-13 13:33:26 +0000 | [diff] [blame] | 256 | *usedPixRefs->append() = bm.pixelRef(); |
robertphillips@google.com | ed9866c | 2014-01-09 19:20:45 +0000 | [diff] [blame] | 257 | } |
| 258 | |
skia.committer@gmail.com | 856673a | 2014-01-10 07:08:11 +0000 | [diff] [blame] | 259 | static void drawtextonpath_proc(SkCanvas* canvas, const SkBitmap& bm, |
robertphillips@google.com | 56bf6e4 | 2014-01-13 13:33:26 +0000 | [diff] [blame] | 260 | const SkBitmap& altBM, const SkPoint& pos, |
| 261 | SkTDArray<SkPixelRef*>* usedPixRefs) { |
robertphillips@google.com | ed9866c | 2014-01-09 19:20:45 +0000 | [diff] [blame] | 262 | SkPaint paint; |
| 263 | |
| 264 | init_paint(&paint, bm); |
| 265 | paint.setTextSize(SkIntToScalar(1.5*bm.width())); |
| 266 | |
| 267 | SkPath path; |
| 268 | path.lineTo(SkIntToScalar(bm.width()), 0); |
| 269 | path.offset(pos.fX, pos.fY+bm.height()); |
| 270 | |
| 271 | canvas->drawTextOnPath("O", 1, path, NULL, paint); |
robertphillips@google.com | 56bf6e4 | 2014-01-13 13:33:26 +0000 | [diff] [blame] | 272 | *usedPixRefs->append() = bm.pixelRef(); |
robertphillips@google.com | ed9866c | 2014-01-09 19:20:45 +0000 | [diff] [blame] | 273 | } |
| 274 | |
skia.committer@gmail.com | 856673a | 2014-01-10 07:08:11 +0000 | [diff] [blame] | 275 | static void drawverts_proc(SkCanvas* canvas, const SkBitmap& bm, |
robertphillips@google.com | 56bf6e4 | 2014-01-13 13:33:26 +0000 | [diff] [blame] | 276 | const SkBitmap& altBM, const SkPoint& pos, |
| 277 | SkTDArray<SkPixelRef*>* usedPixRefs) { |
robertphillips@google.com | ed9866c | 2014-01-09 19:20:45 +0000 | [diff] [blame] | 278 | SkPaint paint; |
| 279 | init_paint(&paint, bm); |
| 280 | |
skia.committer@gmail.com | 2e9a715 | 2014-01-14 07:01:42 +0000 | [diff] [blame] | 281 | SkPoint verts[4] = { |
| 282 | { pos.fX, pos.fY }, |
| 283 | { pos.fX + bm.width(), pos.fY }, |
| 284 | { pos.fX + bm.width(), pos.fY + bm.height() }, |
| 285 | { pos.fX, pos.fY + bm.height() } |
robertphillips@google.com | ed9866c | 2014-01-09 19:20:45 +0000 | [diff] [blame] | 286 | }; |
skia.committer@gmail.com | 856673a | 2014-01-10 07:08:11 +0000 | [diff] [blame] | 287 | SkPoint texs[4] = { { 0, 0 }, |
| 288 | { SkIntToScalar(bm.width()), 0 }, |
| 289 | { SkIntToScalar(bm.width()), SkIntToScalar(bm.height()) }, |
robertphillips@google.com | ed9866c | 2014-01-09 19:20:45 +0000 | [diff] [blame] | 290 | { 0, SkIntToScalar(bm.height()) } }; |
| 291 | uint16_t indices[6] = { 0, 1, 2, 0, 2, 3 }; |
| 292 | |
skia.committer@gmail.com | 856673a | 2014-01-10 07:08:11 +0000 | [diff] [blame] | 293 | canvas->drawVertices(SkCanvas::kTriangles_VertexMode, 4, verts, texs, NULL, NULL, |
robertphillips@google.com | ed9866c | 2014-01-09 19:20:45 +0000 | [diff] [blame] | 294 | indices, 6, paint); |
robertphillips@google.com | 56bf6e4 | 2014-01-13 13:33:26 +0000 | [diff] [blame] | 295 | *usedPixRefs->append() = bm.pixelRef(); |
reed@google.com | fe7b1ed | 2012-11-29 21:00:39 +0000 | [diff] [blame] | 296 | } |
| 297 | |
| 298 | // 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] | 299 | static SkPicture* record_bitmaps(const SkBitmap bm[], |
robertphillips@google.com | 56bf6e4 | 2014-01-13 13:33:26 +0000 | [diff] [blame] | 300 | const SkPoint pos[], |
| 301 | SkTDArray<SkPixelRef*> analytic[], |
skia.committer@gmail.com | 2e9a715 | 2014-01-14 07:01:42 +0000 | [diff] [blame] | 302 | int count, |
robertphillips@google.com | 56bf6e4 | 2014-01-13 13:33:26 +0000 | [diff] [blame] | 303 | DrawBitmapProc proc) { |
reed@google.com | fe7b1ed | 2012-11-29 21:00:39 +0000 | [diff] [blame] | 304 | SkPicture* pic = new SkPicture; |
| 305 | SkCanvas* canvas = pic->beginRecording(1000, 1000); |
| 306 | for (int i = 0; i < count; ++i) { |
robertphillips@google.com | 56bf6e4 | 2014-01-13 13:33:26 +0000 | [diff] [blame] | 307 | analytic[i].rewind(); |
robertphillips@google.com | ed9866c | 2014-01-09 19:20:45 +0000 | [diff] [blame] | 308 | canvas->save(); |
| 309 | SkRect clipRect = SkRect::MakeXYWH(pos[i].fX, pos[i].fY, |
| 310 | SkIntToScalar(bm[i].width()), |
| 311 | SkIntToScalar(bm[i].height())); |
| 312 | canvas->clipRect(clipRect, SkRegion::kIntersect_Op); |
robertphillips@google.com | 56bf6e4 | 2014-01-13 13:33:26 +0000 | [diff] [blame] | 313 | proc(canvas, bm[i], bm[count+i], pos[i], &analytic[i]); |
robertphillips@google.com | ed9866c | 2014-01-09 19:20:45 +0000 | [diff] [blame] | 314 | canvas->restore(); |
reed@google.com | fe7b1ed | 2012-11-29 21:00:39 +0000 | [diff] [blame] | 315 | } |
| 316 | pic->endRecording(); |
| 317 | return pic; |
| 318 | } |
| 319 | |
commit-bot@chromium.org | e0e7cfe | 2013-09-09 20:09:12 +0000 | [diff] [blame] | 320 | 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] | 321 | rect->fLeft = rand.nextRangeScalar(-W, 2*W); |
| 322 | rect->fTop = rand.nextRangeScalar(-H, 2*H); |
| 323 | rect->fRight = rect->fLeft + rand.nextRangeScalar(0, W); |
| 324 | rect->fBottom = rect->fTop + rand.nextRangeScalar(0, H); |
| 325 | |
| 326 | // we integralize rect to make our tests more predictable, since Gather is |
| 327 | // a little sloppy. |
| 328 | SkIRect ir; |
| 329 | rect->round(&ir); |
| 330 | rect->set(ir); |
| 331 | } |
| 332 | |
robertphillips@google.com | ed9866c | 2014-01-09 19:20:45 +0000 | [diff] [blame] | 333 | static void draw(SkPicture* pic, int width, int height, SkBitmap* result) { |
| 334 | make_bm(result, width, height, SK_ColorBLACK, false); |
reed@google.com | fe7b1ed | 2012-11-29 21:00:39 +0000 | [diff] [blame] | 335 | |
| 336 | SkCanvas canvas(*result); |
reed@google.com | fe7b1ed | 2012-11-29 21:00:39 +0000 | [diff] [blame] | 337 | canvas.drawPicture(*pic); |
| 338 | } |
| 339 | |
| 340 | template <typename T> int find_index(const T* array, T elem, int count) { |
| 341 | for (int i = 0; i < count; ++i) { |
| 342 | if (array[i] == elem) { |
| 343 | return i; |
| 344 | } |
| 345 | } |
| 346 | return -1; |
| 347 | } |
| 348 | |
| 349 | // Return true if 'ref' is found in array[] |
| 350 | static bool find(SkPixelRef const * const * array, SkPixelRef const * ref, int count) { |
| 351 | return find_index<const SkPixelRef*>(array, ref, count) >= 0; |
| 352 | } |
| 353 | |
skia.committer@gmail.com | 856673a | 2014-01-10 07:08:11 +0000 | [diff] [blame] | 354 | // Look at each pixel that is inside 'subset', and if its color appears in |
| 355 | // 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] | 356 | // array, skipping duplicates of the same value. |
| 357 | // Note that gathering pixelRefs from rendered colors suffers from the problem |
| 358 | // that multiple simultaneous textures (e.g., A8 for alpha and 8888 for color) |
| 359 | // isn't easy to reconstruct. |
| 360 | static void gather_from_image(const SkBitmap& bm, SkPixelRef* const refs[], |
| 361 | int count, SkTDArray<SkPixelRef*>* array, |
| 362 | const SkRect& subset) { |
| 363 | SkIRect ir; |
| 364 | subset.roundOut(&ir); |
| 365 | |
| 366 | if (!ir.intersect(0, 0, bm.width()-1, bm.height()-1)) { |
| 367 | return; |
| 368 | } |
| 369 | |
reed@google.com | fe7b1ed | 2012-11-29 21:00:39 +0000 | [diff] [blame] | 370 | // Since we only want to return unique values in array, when we scan we just |
| 371 | // set a bit for each index'd color found. In practice we only have a few |
| 372 | // distinct colors, so we just use an int's bits as our array. Hence the |
| 373 | // assert that count <= number-of-bits-in-our-int. |
| 374 | SkASSERT((unsigned)count <= 32); |
| 375 | uint32_t bitarray = 0; |
| 376 | |
| 377 | SkAutoLockPixels alp(bm); |
| 378 | |
robertphillips@google.com | ed9866c | 2014-01-09 19:20:45 +0000 | [diff] [blame] | 379 | for (int y = ir.fTop; y < ir.fBottom; ++y) { |
| 380 | for (int x = ir.fLeft; x < ir.fRight; ++x) { |
reed@google.com | fe7b1ed | 2012-11-29 21:00:39 +0000 | [diff] [blame] | 381 | SkPMColor pmc = *bm.getAddr32(x, y); |
| 382 | // the only good case where the color is not found would be if |
| 383 | // the color is transparent, meaning no bitmap was drawn in that |
| 384 | // pixel. |
| 385 | if (pmc) { |
bsalomon@google.com | c3d753e | 2013-01-08 17:24:44 +0000 | [diff] [blame] | 386 | uint32_t index = SkGetPackedR32(pmc); |
reed@google.com | fe7b1ed | 2012-11-29 21:00:39 +0000 | [diff] [blame] | 387 | SkASSERT(SkGetPackedG32(pmc) == index); |
| 388 | SkASSERT(SkGetPackedB32(pmc) == index); |
robertphillips@google.com | ed9866c | 2014-01-09 19:20:45 +0000 | [diff] [blame] | 389 | if (0 == index) { |
| 390 | continue; // background color |
| 391 | } |
| 392 | SkASSERT(0 == (index - gColorOffset) % gColorScale); |
| 393 | index = (index - gColorOffset) / gColorScale; |
bsalomon@google.com | 5f429b0 | 2013-01-08 18:42:20 +0000 | [diff] [blame] | 394 | SkASSERT(static_cast<int>(index) < count); |
reed@google.com | fe7b1ed | 2012-11-29 21:00:39 +0000 | [diff] [blame] | 395 | bitarray |= 1 << index; |
| 396 | } |
| 397 | } |
| 398 | } |
| 399 | |
| 400 | for (int i = 0; i < count; ++i) { |
| 401 | if (bitarray & (1 << i)) { |
| 402 | *array->append() = refs[i]; |
| 403 | } |
| 404 | } |
| 405 | } |
| 406 | |
robertphillips@google.com | 0e4ce14 | 2014-01-13 13:46:45 +0000 | [diff] [blame] | 407 | 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] | 408 | const SkTDArray<SkPixelRef*> analytic[], |
| 409 | int count, |
| 410 | SkTDArray<SkPixelRef*>* result, |
robertphillips@google.com | 0e4ce14 | 2014-01-13 13:46:45 +0000 | [diff] [blame] | 411 | const SkRect& subset) { |
robertphillips@google.com | 56bf6e4 | 2014-01-13 13:33:26 +0000 | [diff] [blame] | 412 | for (int i = 0; i < count; ++i) { |
| 413 | SkRect rect = SkRect::MakeXYWH(pos[i].fX, pos[i].fY, w, h); |
| 414 | |
| 415 | if (SkRect::Intersects(subset, rect)) { |
| 416 | result->append(analytic[i].count(), analytic[i].begin()); |
| 417 | } |
| 418 | } |
| 419 | } |
| 420 | |
| 421 | static const DrawBitmapProc gProcs[] = { |
| 422 | drawpaint_proc, |
| 423 | drawpoints_proc, |
| 424 | drawrect_proc, |
| 425 | drawoval_proc, |
| 426 | drawrrect_proc, |
| 427 | drawpath_proc, |
skia.committer@gmail.com | 2e9a715 | 2014-01-14 07:01:42 +0000 | [diff] [blame] | 428 | drawbitmap_proc, |
robertphillips@google.com | 56bf6e4 | 2014-01-13 13:33:26 +0000 | [diff] [blame] | 429 | drawbitmap_withshader_proc, |
| 430 | drawsprite_proc, |
| 431 | #if 0 |
| 432 | drawsprite_withshader_proc, |
| 433 | #endif |
skia.committer@gmail.com | 2e9a715 | 2014-01-14 07:01:42 +0000 | [diff] [blame] | 434 | drawbitmaprect_proc, |
robertphillips@google.com | 56bf6e4 | 2014-01-13 13:33:26 +0000 | [diff] [blame] | 435 | drawbitmaprect_withshader_proc, |
| 436 | drawtext_proc, |
| 437 | drawpostext_proc, |
| 438 | drawtextonpath_proc, |
| 439 | drawverts_proc, |
| 440 | }; |
| 441 | |
| 442 | 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] | 443 | // Our convention is that the color components contain an encoding of |
| 444 | // 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] | 445 | // reserved for the background |
| 446 | for (int i = 0; i < num; ++i) { |
skia.committer@gmail.com | 2e9a715 | 2014-01-14 07:01:42 +0000 | [diff] [blame] | 447 | make_bm(&bm[i], w, h, |
| 448 | SkColorSetARGB(0xFF, |
| 449 | gColorScale*i+gColorOffset, |
| 450 | gColorScale*i+gColorOffset, |
| 451 | gColorScale*i+gColorOffset), |
robertphillips@google.com | 56bf6e4 | 2014-01-13 13:33:26 +0000 | [diff] [blame] | 452 | true); |
| 453 | refs[i] = bm[i].pixelRef(); |
| 454 | } |
| 455 | |
| 456 | // The A8 alternate bitmaps are all BW checkerboards |
| 457 | for (int i = 0; i < num; ++i) { |
| 458 | make_checkerboard(&bm[num+i], w, h, true); |
| 459 | refs[num+i] = bm[num+i].pixelRef(); |
| 460 | } |
| 461 | } |
| 462 | |
reed@google.com | fe7b1ed | 2012-11-29 21:00:39 +0000 | [diff] [blame] | 463 | static void test_gatherpixelrefs(skiatest::Reporter* reporter) { |
robertphillips@google.com | ed9866c | 2014-01-09 19:20:45 +0000 | [diff] [blame] | 464 | const int IW = 32; |
reed@google.com | fe7b1ed | 2012-11-29 21:00:39 +0000 | [diff] [blame] | 465 | const int IH = IW; |
| 466 | const SkScalar W = SkIntToScalar(IW); |
| 467 | const SkScalar H = W; |
| 468 | |
| 469 | static const int N = 4; |
robertphillips@google.com | ed9866c | 2014-01-09 19:20:45 +0000 | [diff] [blame] | 470 | SkBitmap bm[2*N]; |
| 471 | SkPixelRef* refs[2*N]; |
robertphillips@google.com | 56bf6e4 | 2014-01-13 13:33:26 +0000 | [diff] [blame] | 472 | SkTDArray<SkPixelRef*> analytic[N]; |
reed@google.com | fe7b1ed | 2012-11-29 21:00:39 +0000 | [diff] [blame] | 473 | |
robertphillips@google.com | ed9866c | 2014-01-09 19:20:45 +0000 | [diff] [blame] | 474 | const SkPoint pos[N] = { |
reed@google.com | fe7b1ed | 2012-11-29 21:00:39 +0000 | [diff] [blame] | 475 | { 0, 0 }, { W, 0 }, { 0, H }, { W, H } |
| 476 | }; |
| 477 | |
robertphillips@google.com | 56bf6e4 | 2014-01-13 13:33:26 +0000 | [diff] [blame] | 478 | create_textures(bm, refs, N, IW, IH); |
reed@google.com | fe7b1ed | 2012-11-29 21:00:39 +0000 | [diff] [blame] | 479 | |
commit-bot@chromium.org | e0e7cfe | 2013-09-09 20:09:12 +0000 | [diff] [blame] | 480 | SkRandom rand; |
robertphillips@google.com | 56bf6e4 | 2014-01-13 13:33:26 +0000 | [diff] [blame] | 481 | for (size_t k = 0; k < SK_ARRAY_COUNT(gProcs); ++k) { |
| 482 | SkAutoTUnref<SkPicture> pic(record_bitmaps(bm, pos, analytic, N, gProcs[k])); |
reed@google.com | fe7b1ed | 2012-11-29 21:00:39 +0000 | [diff] [blame] | 483 | |
tomhudson@google.com | 381010e | 2013-10-24 11:12:47 +0000 | [diff] [blame] | 484 | REPORTER_ASSERT(reporter, pic->willPlayBackBitmaps() || N == 0); |
reed@google.com | fe7b1ed | 2012-11-29 21:00:39 +0000 | [diff] [blame] | 485 | // 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] | 486 | // contain 1 or 2 bitmaps. |
reed@google.com | fe7b1ed | 2012-11-29 21:00:39 +0000 | [diff] [blame] | 487 | for (size_t i = 0; i < SK_ARRAY_COUNT(pos); ++i) { |
| 488 | SkRect r; |
| 489 | r.set(2, 2, W - 2, H - 2); |
| 490 | r.offset(pos[i].fX, pos[i].fY); |
| 491 | SkAutoDataUnref data(SkPictureUtils::GatherPixelRefs(pic, r)); |
| 492 | REPORTER_ASSERT(reporter, data); |
commit-bot@chromium.org | fe433c1 | 2013-07-09 16:04:32 +0000 | [diff] [blame] | 493 | if (data) { |
robertphillips@google.com | ed9866c | 2014-01-09 19:20:45 +0000 | [diff] [blame] | 494 | SkPixelRef** gatheredRefs = (SkPixelRef**)data->data(); |
robertphillips@google.com | e9cd27d | 2013-10-16 17:48:11 +0000 | [diff] [blame] | 495 | int count = static_cast<int>(data->size() / sizeof(SkPixelRef*)); |
robertphillips@google.com | ed9866c | 2014-01-09 19:20:45 +0000 | [diff] [blame] | 496 | REPORTER_ASSERT(reporter, 1 == count || 2 == count); |
| 497 | if (1 == count) { |
| 498 | REPORTER_ASSERT(reporter, gatheredRefs[0] == refs[i]); |
| 499 | } else if (2 == count) { |
skia.committer@gmail.com | 856673a | 2014-01-10 07:08:11 +0000 | [diff] [blame] | 500 | REPORTER_ASSERT(reporter, |
robertphillips@google.com | ed9866c | 2014-01-09 19:20:45 +0000 | [diff] [blame] | 501 | (gatheredRefs[0] == refs[i] && gatheredRefs[1] == refs[i+N]) || |
| 502 | (gatheredRefs[1] == refs[i] && gatheredRefs[0] == refs[i+N])); |
| 503 | } |
commit-bot@chromium.org | fe433c1 | 2013-07-09 16:04:32 +0000 | [diff] [blame] | 504 | } |
reed@google.com | fe7b1ed | 2012-11-29 21:00:39 +0000 | [diff] [blame] | 505 | } |
| 506 | |
robertphillips@google.com | ed9866c | 2014-01-09 19:20:45 +0000 | [diff] [blame] | 507 | SkBitmap image; |
| 508 | draw(pic, 2*IW, 2*IH, &image); |
| 509 | |
reed@google.com | fe7b1ed | 2012-11-29 21:00:39 +0000 | [diff] [blame] | 510 | // Test a bunch of random (mostly) rects, and compare the gather results |
| 511 | // with a deduced list of refs by looking at the colors drawn. |
| 512 | for (int j = 0; j < 100; ++j) { |
| 513 | SkRect r; |
| 514 | rand_rect(&r, rand, 2*W, 2*H); |
| 515 | |
robertphillips@google.com | 56bf6e4 | 2014-01-13 13:33:26 +0000 | [diff] [blame] | 516 | SkTDArray<SkPixelRef*> fromImage; |
| 517 | gather_from_image(image, refs, N, &fromImage, r); |
| 518 | |
| 519 | SkTDArray<SkPixelRef*> fromAnalytic; |
| 520 | gather_from_analytic(pos, W, H, analytic, N, &fromAnalytic, r); |
reed@google.com | fe7b1ed | 2012-11-29 21:00:39 +0000 | [diff] [blame] | 521 | |
| 522 | SkData* data = SkPictureUtils::GatherPixelRefs(pic, r); |
| 523 | size_t dataSize = data ? data->size() : 0; |
robertphillips@google.com | e9cd27d | 2013-10-16 17:48:11 +0000 | [diff] [blame] | 524 | int gatherCount = static_cast<int>(dataSize / sizeof(SkPixelRef*)); |
reed@google.com | fe7b1ed | 2012-11-29 21:00:39 +0000 | [diff] [blame] | 525 | SkASSERT(gatherCount * sizeof(SkPixelRef*) == dataSize); |
| 526 | SkPixelRef** gatherRefs = data ? (SkPixelRef**)(data->data()) : NULL; |
| 527 | SkAutoDataUnref adu(data); |
| 528 | |
robertphillips@google.com | 56bf6e4 | 2014-01-13 13:33:26 +0000 | [diff] [blame] | 529 | // Everything that we saw drawn should appear in the analytic list |
| 530 | // but the analytic list may contain some pixelRefs that were not |
| 531 | // seen in the image (e.g., A8 textures used as masks) |
| 532 | for (int i = 0; i < fromImage.count(); ++i) { |
| 533 | REPORTER_ASSERT(reporter, -1 != fromAnalytic.find(fromImage[i])); |
| 534 | } |
skia.committer@gmail.com | c3d7d90 | 2012-11-30 02:01:24 +0000 | [diff] [blame] | 535 | |
reed@google.com | fe7b1ed | 2012-11-29 21:00:39 +0000 | [diff] [blame] | 536 | /* |
| 537 | * GatherPixelRefs is conservative, so it can return more bitmaps |
robertphillips@google.com | 56bf6e4 | 2014-01-13 13:33:26 +0000 | [diff] [blame] | 538 | * than are strictly required. Thus our check here is only that |
| 539 | * 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] | 540 | * a strict requirement on Gather, which is meant to be quick and |
| 541 | * only mostly-correct, but at the moment this test should work. |
| 542 | */ |
robertphillips@google.com | 56bf6e4 | 2014-01-13 13:33:26 +0000 | [diff] [blame] | 543 | for (int i = 0; i < fromAnalytic.count(); ++i) { |
| 544 | bool found = find(gatherRefs, fromAnalytic[i], gatherCount); |
reed@google.com | fe7b1ed | 2012-11-29 21:00:39 +0000 | [diff] [blame] | 545 | REPORTER_ASSERT(reporter, found); |
| 546 | #if 0 |
| 547 | // enable this block of code to debug failures, as it will rerun |
| 548 | // the case that failed. |
| 549 | if (!found) { |
| 550 | SkData* data = SkPictureUtils::GatherPixelRefs(pic, r); |
| 551 | size_t dataSize = data ? data->size() : 0; |
| 552 | } |
| 553 | #endif |
| 554 | } |
| 555 | } |
| 556 | } |
| 557 | } |
| 558 | |
robertphillips@google.com | 56bf6e4 | 2014-01-13 13:33:26 +0000 | [diff] [blame] | 559 | static void test_gatherpixelrefsandrects(skiatest::Reporter* reporter) { |
| 560 | const int IW = 32; |
| 561 | const int IH = IW; |
| 562 | const SkScalar W = SkIntToScalar(IW); |
| 563 | const SkScalar H = W; |
| 564 | |
| 565 | static const int N = 4; |
| 566 | SkBitmap bm[2*N]; |
| 567 | SkPixelRef* refs[2*N]; |
| 568 | SkTDArray<SkPixelRef*> analytic[N]; |
| 569 | |
| 570 | const SkPoint pos[N] = { |
| 571 | { 0, 0 }, { W, 0 }, { 0, H }, { W, H } |
| 572 | }; |
| 573 | |
| 574 | create_textures(bm, refs, N, IW, IH); |
| 575 | |
| 576 | SkRandom rand; |
| 577 | for (size_t k = 0; k < SK_ARRAY_COUNT(gProcs); ++k) { |
| 578 | SkAutoTUnref<SkPicture> pic(record_bitmaps(bm, pos, analytic, N, gProcs[k])); |
| 579 | |
| 580 | REPORTER_ASSERT(reporter, pic->willPlayBackBitmaps() || N == 0); |
| 581 | |
| 582 | SkAutoTUnref<SkPictureUtils::SkPixelRefContainer> prCont( |
| 583 | new SkPictureUtils::SkPixelRefsAndRectsList); |
| 584 | |
| 585 | SkPictureUtils::GatherPixelRefsAndRects(pic, prCont); |
| 586 | |
| 587 | // quick check for a small piece of each quadrant, which should just |
| 588 | // contain 1 or 2 bitmaps. |
| 589 | for (size_t i = 0; i < SK_ARRAY_COUNT(pos); ++i) { |
| 590 | SkRect r; |
| 591 | r.set(2, 2, W - 2, H - 2); |
| 592 | r.offset(pos[i].fX, pos[i].fY); |
| 593 | |
| 594 | SkTDArray<SkPixelRef*> gatheredRefs; |
| 595 | prCont->query(r, &gatheredRefs); |
| 596 | |
| 597 | int count = gatheredRefs.count(); |
| 598 | REPORTER_ASSERT(reporter, 1 == count || 2 == count); |
| 599 | if (1 == count) { |
| 600 | REPORTER_ASSERT(reporter, gatheredRefs[0] == refs[i]); |
| 601 | } else if (2 == count) { |
skia.committer@gmail.com | 2e9a715 | 2014-01-14 07:01:42 +0000 | [diff] [blame] | 602 | REPORTER_ASSERT(reporter, |
robertphillips@google.com | 56bf6e4 | 2014-01-13 13:33:26 +0000 | [diff] [blame] | 603 | (gatheredRefs[0] == refs[i] && gatheredRefs[1] == refs[i+N]) || |
| 604 | (gatheredRefs[1] == refs[i] && gatheredRefs[0] == refs[i+N])); |
| 605 | } |
| 606 | } |
| 607 | |
| 608 | SkBitmap image; |
| 609 | draw(pic, 2*IW, 2*IH, &image); |
| 610 | |
| 611 | // Test a bunch of random (mostly) rects, and compare the gather results |
| 612 | // with the analytic results and the pixel refs seen in a rendering. |
| 613 | for (int j = 0; j < 100; ++j) { |
| 614 | SkRect r; |
| 615 | rand_rect(&r, rand, 2*W, 2*H); |
| 616 | |
| 617 | SkTDArray<SkPixelRef*> fromImage; |
| 618 | gather_from_image(image, refs, N, &fromImage, r); |
| 619 | |
| 620 | SkTDArray<SkPixelRef*> fromAnalytic; |
| 621 | gather_from_analytic(pos, W, H, analytic, N, &fromAnalytic, r); |
| 622 | |
| 623 | SkTDArray<SkPixelRef*> gatheredRefs; |
| 624 | prCont->query(r, &gatheredRefs); |
| 625 | |
| 626 | // Everything that we saw drawn should appear in the analytic list |
| 627 | // but the analytic list may contain some pixelRefs that were not |
| 628 | // seen in the image (e.g., A8 textures used as masks) |
| 629 | for (int i = 0; i < fromImage.count(); ++i) { |
| 630 | REPORTER_ASSERT(reporter, -1 != fromAnalytic.find(fromImage[i])); |
| 631 | } |
| 632 | |
| 633 | // Everything in the analytic list should appear in the gathered |
skia.committer@gmail.com | 2e9a715 | 2014-01-14 07:01:42 +0000 | [diff] [blame] | 634 | // list. |
robertphillips@google.com | 56bf6e4 | 2014-01-13 13:33:26 +0000 | [diff] [blame] | 635 | for (int i = 0; i < fromAnalytic.count(); ++i) { |
| 636 | REPORTER_ASSERT(reporter, -1 != gatheredRefs.find(fromAnalytic[i])); |
| 637 | } |
| 638 | } |
| 639 | } |
| 640 | } |
| 641 | |
scroggo@google.com | d614c6a | 2012-09-14 17:26:37 +0000 | [diff] [blame] | 642 | #ifdef SK_DEBUG |
| 643 | // Ensure that deleting SkPicturePlayback does not assert. Asserts only fire in debug mode, so only |
| 644 | // run in debug mode. |
| 645 | static void test_deleting_empty_playback() { |
| 646 | SkPicture picture; |
| 647 | // Creates an SkPictureRecord |
| 648 | picture.beginRecording(0, 0); |
| 649 | // Turns that into an SkPicturePlayback |
| 650 | picture.endRecording(); |
| 651 | // Deletes the old SkPicturePlayback, and creates a new SkPictureRecord |
| 652 | picture.beginRecording(0, 0); |
| 653 | } |
| 654 | |
| 655 | // Ensure that serializing an empty picture does not assert. Likewise only runs in debug mode. |
| 656 | static void test_serializing_empty_picture() { |
| 657 | SkPicture picture; |
| 658 | picture.beginRecording(0, 0); |
| 659 | picture.endRecording(); |
| 660 | SkDynamicMemoryWStream stream; |
| 661 | picture.serialize(&stream); |
| 662 | } |
| 663 | #endif |
| 664 | |
commit-bot@chromium.org | e0e7cfe | 2013-09-09 20:09:12 +0000 | [diff] [blame] | 665 | static void rand_op(SkCanvas* canvas, SkRandom& rand) { |
reed@google.com | 21b519d | 2012-10-02 17:42:15 +0000 | [diff] [blame] | 666 | SkPaint paint; |
| 667 | SkRect rect = SkRect::MakeWH(50, 50); |
| 668 | |
| 669 | SkScalar unit = rand.nextUScalar1(); |
| 670 | if (unit <= 0.3) { |
| 671 | // SkDebugf("save\n"); |
| 672 | canvas->save(); |
| 673 | } else if (unit <= 0.6) { |
| 674 | // SkDebugf("restore\n"); |
| 675 | canvas->restore(); |
| 676 | } else if (unit <= 0.9) { |
| 677 | // SkDebugf("clip\n"); |
| 678 | canvas->clipRect(rect); |
| 679 | } else { |
| 680 | // SkDebugf("draw\n"); |
| 681 | canvas->drawPaint(paint); |
| 682 | } |
| 683 | } |
| 684 | |
sugoi@google.com | 54f0d1b | 2013-02-27 19:17:41 +0000 | [diff] [blame] | 685 | static void test_peephole() { |
commit-bot@chromium.org | e0e7cfe | 2013-09-09 20:09:12 +0000 | [diff] [blame] | 686 | SkRandom rand; |
reed@google.com | 21b519d | 2012-10-02 17:42:15 +0000 | [diff] [blame] | 687 | |
| 688 | for (int j = 0; j < 100; j++) { |
commit-bot@chromium.org | e0e7cfe | 2013-09-09 20:09:12 +0000 | [diff] [blame] | 689 | SkRandom rand2(rand); // remember the seed |
reed@google.com | 21b519d | 2012-10-02 17:42:15 +0000 | [diff] [blame] | 690 | |
| 691 | SkPicture picture; |
| 692 | SkCanvas* canvas = picture.beginRecording(100, 100); |
| 693 | |
| 694 | for (int i = 0; i < 1000; ++i) { |
| 695 | rand_op(canvas, rand); |
| 696 | } |
| 697 | picture.endRecording(); |
jvanverth@google.com | c490f80 | 2013-03-04 13:56:38 +0000 | [diff] [blame] | 698 | |
| 699 | rand = rand2; |
reed@google.com | 21b519d | 2012-10-02 17:42:15 +0000 | [diff] [blame] | 700 | } |
| 701 | |
| 702 | { |
| 703 | SkPicture picture; |
| 704 | SkCanvas* canvas = picture.beginRecording(100, 100); |
| 705 | SkRect rect = SkRect::MakeWH(50, 50); |
skia.committer@gmail.com | 52c2437 | 2012-10-03 02:01:13 +0000 | [diff] [blame] | 706 | |
reed@google.com | 21b519d | 2012-10-02 17:42:15 +0000 | [diff] [blame] | 707 | for (int i = 0; i < 100; ++i) { |
| 708 | canvas->save(); |
| 709 | } |
| 710 | while (canvas->getSaveCount() > 1) { |
| 711 | canvas->clipRect(rect); |
| 712 | canvas->restore(); |
| 713 | } |
| 714 | picture.endRecording(); |
| 715 | } |
| 716 | } |
| 717 | |
scroggo@google.com | 4b90b11 | 2012-12-04 15:08:56 +0000 | [diff] [blame] | 718 | #ifndef SK_DEBUG |
| 719 | // Only test this is in release mode. We deliberately crash in debug mode, since a valid caller |
| 720 | // should never do this. |
| 721 | static void test_bad_bitmap() { |
| 722 | // This bitmap has a width and height but no pixels. As a result, attempting to record it will |
| 723 | // fail. |
| 724 | SkBitmap bm; |
| 725 | bm.setConfig(SkBitmap::kARGB_8888_Config, 100, 100); |
| 726 | SkPicture picture; |
| 727 | SkCanvas* recordingCanvas = picture.beginRecording(100, 100); |
| 728 | recordingCanvas->drawBitmap(bm, 0, 0); |
| 729 | picture.endRecording(); |
| 730 | |
| 731 | SkCanvas canvas; |
| 732 | canvas.drawPicture(picture); |
| 733 | } |
| 734 | #endif |
| 735 | |
reed@google.com | 672588b | 2014-01-08 15:42:01 +0000 | [diff] [blame] | 736 | static SkData* encode_bitmap_to_data(size_t*, const SkBitmap& bm) { |
scroggo@google.com | 1b1bcc3 | 2013-05-21 20:31:23 +0000 | [diff] [blame] | 737 | return SkImageEncoder::EncodeData(bm, SkImageEncoder::kPNG_Type, 100); |
scroggo@google.com | 7c9d539 | 2012-12-10 15:40:55 +0000 | [diff] [blame] | 738 | } |
| 739 | |
| 740 | static SkData* serialized_picture_from_bitmap(const SkBitmap& bitmap) { |
| 741 | SkPicture picture; |
| 742 | SkCanvas* canvas = picture.beginRecording(bitmap.width(), bitmap.height()); |
| 743 | canvas->drawBitmap(bitmap, 0, 0); |
| 744 | SkDynamicMemoryWStream wStream; |
scroggo@google.com | 1b1bcc3 | 2013-05-21 20:31:23 +0000 | [diff] [blame] | 745 | picture.serialize(&wStream, &encode_bitmap_to_data); |
scroggo@google.com | 7c9d539 | 2012-12-10 15:40:55 +0000 | [diff] [blame] | 746 | return wStream.copyToData(); |
| 747 | } |
| 748 | |
scroggo@google.com | 49ce11b | 2013-04-25 18:29:32 +0000 | [diff] [blame] | 749 | struct ErrorContext { |
| 750 | int fErrors; |
| 751 | skiatest::Reporter* fReporter; |
| 752 | }; |
| 753 | |
| 754 | static void assert_one_parse_error_cb(SkError error, void* context) { |
| 755 | ErrorContext* errorContext = static_cast<ErrorContext*>(context); |
| 756 | errorContext->fErrors++; |
| 757 | // This test only expects one error, and that is a kParseError. If there are others, |
| 758 | // there is some unknown problem. |
| 759 | REPORTER_ASSERT_MESSAGE(errorContext->fReporter, 1 == errorContext->fErrors, |
| 760 | "This threw more errors than expected."); |
| 761 | REPORTER_ASSERT_MESSAGE(errorContext->fReporter, kParseError_SkError == error, |
| 762 | SkGetLastErrorString()); |
| 763 | } |
| 764 | |
scroggo@google.com | 7c9d539 | 2012-12-10 15:40:55 +0000 | [diff] [blame] | 765 | static void test_bitmap_with_encoded_data(skiatest::Reporter* reporter) { |
| 766 | // Create a bitmap that will be encoded. |
| 767 | SkBitmap original; |
| 768 | make_bm(&original, 100, 100, SK_ColorBLUE, true); |
| 769 | SkDynamicMemoryWStream wStream; |
| 770 | if (!SkImageEncoder::EncodeStream(&wStream, original, SkImageEncoder::kPNG_Type, 100)) { |
| 771 | return; |
| 772 | } |
| 773 | SkAutoDataUnref data(wStream.copyToData()); |
scroggo@google.com | 7c9d539 | 2012-12-10 15:40:55 +0000 | [diff] [blame] | 774 | |
scroggo@google.com | 7c9d539 | 2012-12-10 15:40:55 +0000 | [diff] [blame] | 775 | SkBitmap bm; |
halcanary@google.com | 3d50ea1 | 2014-01-02 13:15:13 +0000 | [diff] [blame] | 776 | bool installSuccess = SkInstallDiscardablePixelRef( |
| 777 | SkDecodingImageGenerator::Create(data, SkDecodingImageGenerator::Options()), &bm, NULL); |
reed@google.com | bf79023 | 2013-12-13 19:45:58 +0000 | [diff] [blame] | 778 | REPORTER_ASSERT(reporter, installSuccess); |
scroggo@google.com | 7c9d539 | 2012-12-10 15:40:55 +0000 | [diff] [blame] | 779 | |
| 780 | // Write both bitmaps to pictures, and ensure that the resulting data streams are the same. |
| 781 | // Flattening original will follow the old path of performing an encode, while flattening bm |
| 782 | // will use the already encoded data. |
| 783 | SkAutoDataUnref picture1(serialized_picture_from_bitmap(original)); |
| 784 | SkAutoDataUnref picture2(serialized_picture_from_bitmap(bm)); |
| 785 | REPORTER_ASSERT(reporter, picture1->equals(picture2)); |
scroggo@google.com | 49ce11b | 2013-04-25 18:29:32 +0000 | [diff] [blame] | 786 | // Now test that a parse error was generated when trying to create a new SkPicture without |
| 787 | // providing a function to decode the bitmap. |
| 788 | ErrorContext context; |
| 789 | context.fErrors = 0; |
| 790 | context.fReporter = reporter; |
| 791 | SkSetErrorCallback(assert_one_parse_error_cb, &context); |
| 792 | SkMemoryStream pictureStream(picture1); |
scroggo@google.com | 49ce11b | 2013-04-25 18:29:32 +0000 | [diff] [blame] | 793 | SkClearLastError(); |
scroggo@google.com | f1754ec | 2013-06-28 21:32:00 +0000 | [diff] [blame] | 794 | SkAutoUnref pictureFromStream(SkPicture::CreateFromStream(&pictureStream, NULL)); |
| 795 | REPORTER_ASSERT(reporter, pictureFromStream.get() != NULL); |
scroggo@google.com | 49ce11b | 2013-04-25 18:29:32 +0000 | [diff] [blame] | 796 | SkClearLastError(); |
| 797 | SkSetErrorCallback(NULL, NULL); |
scroggo@google.com | 7c9d539 | 2012-12-10 15:40:55 +0000 | [diff] [blame] | 798 | } |
| 799 | |
junov@chromium.org | 94f20dc | 2013-01-28 21:04:44 +0000 | [diff] [blame] | 800 | static void test_clone_empty(skiatest::Reporter* reporter) { |
| 801 | // This is a regression test for crbug.com/172062 |
| 802 | // Before the fix, we used to crash accessing a null pointer when we |
| 803 | // had a picture with no paints. This test passes by not crashing. |
| 804 | { |
| 805 | SkPicture picture; |
| 806 | picture.beginRecording(1, 1); |
| 807 | picture.endRecording(); |
| 808 | SkPicture* destPicture = picture.clone(); |
| 809 | REPORTER_ASSERT(reporter, NULL != destPicture); |
| 810 | destPicture->unref(); |
| 811 | } |
| 812 | { |
| 813 | // Test without call to endRecording |
| 814 | SkPicture picture; |
| 815 | picture.beginRecording(1, 1); |
| 816 | SkPicture* destPicture = picture.clone(); |
| 817 | REPORTER_ASSERT(reporter, NULL != destPicture); |
| 818 | destPicture->unref(); |
| 819 | } |
| 820 | } |
| 821 | |
junov@chromium.org | d575eed | 2013-05-08 15:39:13 +0000 | [diff] [blame] | 822 | static void test_clip_bound_opt(skiatest::Reporter* reporter) { |
| 823 | // Test for crbug.com/229011 |
| 824 | SkRect rect1 = SkRect::MakeXYWH(SkIntToScalar(4), SkIntToScalar(4), |
| 825 | SkIntToScalar(2), SkIntToScalar(2)); |
| 826 | SkRect rect2 = SkRect::MakeXYWH(SkIntToScalar(7), SkIntToScalar(7), |
| 827 | SkIntToScalar(1), SkIntToScalar(1)); |
| 828 | SkRect rect3 = SkRect::MakeXYWH(SkIntToScalar(6), SkIntToScalar(6), |
| 829 | SkIntToScalar(1), SkIntToScalar(1)); |
| 830 | |
| 831 | SkPath invPath; |
| 832 | invPath.addOval(rect1); |
| 833 | invPath.setFillType(SkPath::kInverseEvenOdd_FillType); |
| 834 | SkPath path; |
| 835 | path.addOval(rect2); |
| 836 | SkPath path2; |
| 837 | path2.addOval(rect3); |
| 838 | SkIRect clipBounds; |
| 839 | // Minimalist test set for 100% code coverage of |
| 840 | // SkPictureRecord::updateClipConservativelyUsingBounds |
| 841 | { |
| 842 | SkPicture picture; |
| 843 | SkCanvas* canvas = picture.beginRecording(10, 10, |
| 844 | SkPicture::kUsePathBoundsForClip_RecordingFlag); |
| 845 | canvas->clipPath(invPath, SkRegion::kIntersect_Op); |
| 846 | bool nonEmpty = canvas->getClipDeviceBounds(&clipBounds); |
| 847 | REPORTER_ASSERT(reporter, true == nonEmpty); |
| 848 | REPORTER_ASSERT(reporter, 0 == clipBounds.fLeft); |
| 849 | REPORTER_ASSERT(reporter, 0 == clipBounds.fTop); |
| 850 | REPORTER_ASSERT(reporter, 10 == clipBounds.fBottom); |
| 851 | REPORTER_ASSERT(reporter, 10 == clipBounds.fRight); |
| 852 | } |
| 853 | { |
| 854 | SkPicture picture; |
| 855 | SkCanvas* canvas = picture.beginRecording(10, 10, |
| 856 | SkPicture::kUsePathBoundsForClip_RecordingFlag); |
| 857 | canvas->clipPath(path, SkRegion::kIntersect_Op); |
| 858 | canvas->clipPath(invPath, SkRegion::kIntersect_Op); |
| 859 | bool nonEmpty = canvas->getClipDeviceBounds(&clipBounds); |
| 860 | REPORTER_ASSERT(reporter, true == nonEmpty); |
| 861 | REPORTER_ASSERT(reporter, 7 == clipBounds.fLeft); |
| 862 | REPORTER_ASSERT(reporter, 7 == clipBounds.fTop); |
| 863 | REPORTER_ASSERT(reporter, 8 == clipBounds.fBottom); |
| 864 | REPORTER_ASSERT(reporter, 8 == clipBounds.fRight); |
| 865 | } |
| 866 | { |
| 867 | SkPicture picture; |
| 868 | SkCanvas* canvas = picture.beginRecording(10, 10, |
| 869 | SkPicture::kUsePathBoundsForClip_RecordingFlag); |
| 870 | canvas->clipPath(path, SkRegion::kIntersect_Op); |
| 871 | canvas->clipPath(invPath, SkRegion::kUnion_Op); |
| 872 | bool nonEmpty = canvas->getClipDeviceBounds(&clipBounds); |
| 873 | REPORTER_ASSERT(reporter, true == nonEmpty); |
| 874 | REPORTER_ASSERT(reporter, 0 == clipBounds.fLeft); |
| 875 | REPORTER_ASSERT(reporter, 0 == clipBounds.fTop); |
| 876 | REPORTER_ASSERT(reporter, 10 == clipBounds.fBottom); |
| 877 | REPORTER_ASSERT(reporter, 10 == clipBounds.fRight); |
| 878 | } |
| 879 | { |
| 880 | SkPicture picture; |
| 881 | SkCanvas* canvas = picture.beginRecording(10, 10, |
| 882 | SkPicture::kUsePathBoundsForClip_RecordingFlag); |
| 883 | canvas->clipPath(path, SkRegion::kDifference_Op); |
| 884 | bool nonEmpty = canvas->getClipDeviceBounds(&clipBounds); |
| 885 | REPORTER_ASSERT(reporter, true == nonEmpty); |
| 886 | REPORTER_ASSERT(reporter, 0 == clipBounds.fLeft); |
| 887 | REPORTER_ASSERT(reporter, 0 == clipBounds.fTop); |
| 888 | REPORTER_ASSERT(reporter, 10 == clipBounds.fBottom); |
| 889 | REPORTER_ASSERT(reporter, 10 == clipBounds.fRight); |
| 890 | } |
| 891 | { |
| 892 | SkPicture picture; |
| 893 | SkCanvas* canvas = picture.beginRecording(10, 10, |
| 894 | SkPicture::kUsePathBoundsForClip_RecordingFlag); |
| 895 | canvas->clipPath(path, SkRegion::kReverseDifference_Op); |
| 896 | bool nonEmpty = canvas->getClipDeviceBounds(&clipBounds); |
| 897 | // True clip is actually empty in this case, but the best |
| 898 | // determination we can make using only bounds as input is that the |
| 899 | // clip is included in the bounds of 'path'. |
| 900 | REPORTER_ASSERT(reporter, true == nonEmpty); |
| 901 | REPORTER_ASSERT(reporter, 7 == clipBounds.fLeft); |
| 902 | REPORTER_ASSERT(reporter, 7 == clipBounds.fTop); |
| 903 | REPORTER_ASSERT(reporter, 8 == clipBounds.fBottom); |
| 904 | REPORTER_ASSERT(reporter, 8 == clipBounds.fRight); |
| 905 | } |
| 906 | { |
| 907 | SkPicture picture; |
| 908 | SkCanvas* canvas = picture.beginRecording(10, 10, |
| 909 | SkPicture::kUsePathBoundsForClip_RecordingFlag); |
| 910 | canvas->clipPath(path, SkRegion::kIntersect_Op); |
| 911 | canvas->clipPath(path2, SkRegion::kXOR_Op); |
| 912 | bool nonEmpty = canvas->getClipDeviceBounds(&clipBounds); |
| 913 | REPORTER_ASSERT(reporter, true == nonEmpty); |
| 914 | REPORTER_ASSERT(reporter, 6 == clipBounds.fLeft); |
| 915 | REPORTER_ASSERT(reporter, 6 == clipBounds.fTop); |
| 916 | REPORTER_ASSERT(reporter, 8 == clipBounds.fBottom); |
| 917 | REPORTER_ASSERT(reporter, 8 == clipBounds.fRight); |
| 918 | } |
| 919 | } |
| 920 | |
fmalita@google.com | d0f1a4f | 2013-08-27 15:50:19 +0000 | [diff] [blame] | 921 | /** |
| 922 | * A canvas that records the number of clip commands. |
| 923 | */ |
| 924 | class ClipCountingCanvas : public SkCanvas { |
| 925 | public: |
robertphillips@google.com | 1f2f338 | 2013-08-29 11:54:56 +0000 | [diff] [blame] | 926 | explicit ClipCountingCanvas(SkBaseDevice* device) |
fmalita@google.com | d0f1a4f | 2013-08-27 15:50:19 +0000 | [diff] [blame] | 927 | : SkCanvas(device) |
| 928 | , fClipCount(0){ |
| 929 | } |
| 930 | |
| 931 | virtual bool clipRect(const SkRect& r, SkRegion::Op op, bool doAA) |
| 932 | SK_OVERRIDE { |
| 933 | fClipCount += 1; |
| 934 | return this->INHERITED::clipRect(r, op, doAA); |
| 935 | } |
| 936 | |
| 937 | virtual bool clipRRect(const SkRRect& rrect, SkRegion::Op op, bool doAA) |
| 938 | SK_OVERRIDE { |
| 939 | fClipCount += 1; |
| 940 | return this->INHERITED::clipRRect(rrect, op, doAA); |
| 941 | } |
| 942 | |
| 943 | virtual bool clipPath(const SkPath& path, SkRegion::Op op, bool doAA) |
| 944 | SK_OVERRIDE { |
| 945 | fClipCount += 1; |
| 946 | return this->INHERITED::clipPath(path, op, doAA); |
| 947 | } |
| 948 | |
| 949 | unsigned getClipCount() const { return fClipCount; } |
| 950 | |
| 951 | private: |
| 952 | unsigned fClipCount; |
| 953 | |
| 954 | typedef SkCanvas INHERITED; |
| 955 | }; |
| 956 | |
| 957 | static void test_clip_expansion(skiatest::Reporter* reporter) { |
| 958 | SkPicture picture; |
| 959 | SkCanvas* canvas = picture.beginRecording(10, 10, 0); |
| 960 | |
| 961 | canvas->clipRect(SkRect::MakeEmpty(), SkRegion::kReplace_Op); |
| 962 | // The following expanding clip should not be skipped. |
| 963 | canvas->clipRect(SkRect::MakeXYWH(4, 4, 3, 3), SkRegion::kUnion_Op); |
| 964 | // Draw something so the optimizer doesn't just fold the world. |
| 965 | SkPaint p; |
| 966 | p.setColor(SK_ColorBLUE); |
| 967 | canvas->drawPaint(p); |
| 968 | |
robertphillips@google.com | 1f2f338 | 2013-08-29 11:54:56 +0000 | [diff] [blame] | 969 | SkBitmapDevice testDevice(SkBitmap::kNo_Config, 10, 10); |
fmalita@google.com | d0f1a4f | 2013-08-27 15:50:19 +0000 | [diff] [blame] | 970 | ClipCountingCanvas testCanvas(&testDevice); |
| 971 | picture.draw(&testCanvas); |
| 972 | |
| 973 | // Both clips should be present on playback. |
| 974 | REPORTER_ASSERT(reporter, testCanvas.getClipCount() == 2); |
| 975 | } |
| 976 | |
tomhudson@google.com | 381010e | 2013-10-24 11:12:47 +0000 | [diff] [blame] | 977 | static void test_hierarchical(skiatest::Reporter* reporter) { |
| 978 | SkBitmap bm; |
| 979 | make_bm(&bm, 10, 10, SK_ColorRED, true); |
| 980 | |
| 981 | SkCanvas* canvas; |
| 982 | |
| 983 | SkPicture childPlain; |
| 984 | childPlain.beginRecording(10, 10); |
| 985 | childPlain.endRecording(); |
| 986 | REPORTER_ASSERT(reporter, !childPlain.willPlayBackBitmaps()); // 0 |
| 987 | |
| 988 | SkPicture childWithBitmap; |
| 989 | childWithBitmap.beginRecording(10, 10)->drawBitmap(bm, 0, 0); |
| 990 | childWithBitmap.endRecording(); |
| 991 | REPORTER_ASSERT(reporter, childWithBitmap.willPlayBackBitmaps()); // 1 |
| 992 | |
| 993 | SkPicture parentPP; |
| 994 | canvas = parentPP.beginRecording(10, 10); |
| 995 | canvas->drawPicture(childPlain); |
| 996 | parentPP.endRecording(); |
| 997 | REPORTER_ASSERT(reporter, !parentPP.willPlayBackBitmaps()); // 0 |
| 998 | |
| 999 | SkPicture parentPWB; |
| 1000 | canvas = parentPWB.beginRecording(10, 10); |
| 1001 | canvas->drawPicture(childWithBitmap); |
| 1002 | parentPWB.endRecording(); |
| 1003 | REPORTER_ASSERT(reporter, parentPWB.willPlayBackBitmaps()); // 1 |
| 1004 | |
| 1005 | SkPicture parentWBP; |
| 1006 | canvas = parentWBP.beginRecording(10, 10); |
| 1007 | canvas->drawBitmap(bm, 0, 0); |
| 1008 | canvas->drawPicture(childPlain); |
| 1009 | parentWBP.endRecording(); |
| 1010 | REPORTER_ASSERT(reporter, parentWBP.willPlayBackBitmaps()); // 1 |
| 1011 | |
| 1012 | SkPicture parentWBWB; |
| 1013 | canvas = parentWBWB.beginRecording(10, 10); |
| 1014 | canvas->drawBitmap(bm, 0, 0); |
| 1015 | canvas->drawPicture(childWithBitmap); |
| 1016 | parentWBWB.endRecording(); |
| 1017 | REPORTER_ASSERT(reporter, parentWBWB.willPlayBackBitmaps()); // 2 |
| 1018 | } |
| 1019 | |
tfarina@chromium.org | e4fafb1 | 2013-12-12 21:11:12 +0000 | [diff] [blame] | 1020 | DEF_TEST(Picture, reporter) { |
scroggo@google.com | d614c6a | 2012-09-14 17:26:37 +0000 | [diff] [blame] | 1021 | #ifdef SK_DEBUG |
| 1022 | test_deleting_empty_playback(); |
| 1023 | test_serializing_empty_picture(); |
scroggo@google.com | 4b90b11 | 2012-12-04 15:08:56 +0000 | [diff] [blame] | 1024 | #else |
| 1025 | test_bad_bitmap(); |
scroggo@google.com | d614c6a | 2012-09-14 17:26:37 +0000 | [diff] [blame] | 1026 | #endif |
sugoi@google.com | 54f0d1b | 2013-02-27 19:17:41 +0000 | [diff] [blame] | 1027 | test_peephole(); |
reed@google.com | fe7b1ed | 2012-11-29 21:00:39 +0000 | [diff] [blame] | 1028 | test_gatherpixelrefs(reporter); |
robertphillips@google.com | 56bf6e4 | 2014-01-13 13:33:26 +0000 | [diff] [blame] | 1029 | test_gatherpixelrefsandrects(reporter); |
scroggo@google.com | 7c9d539 | 2012-12-10 15:40:55 +0000 | [diff] [blame] | 1030 | test_bitmap_with_encoded_data(reporter); |
junov@chromium.org | 94f20dc | 2013-01-28 21:04:44 +0000 | [diff] [blame] | 1031 | test_clone_empty(reporter); |
junov@chromium.org | d575eed | 2013-05-08 15:39:13 +0000 | [diff] [blame] | 1032 | test_clip_bound_opt(reporter); |
fmalita@google.com | d0f1a4f | 2013-08-27 15:50:19 +0000 | [diff] [blame] | 1033 | test_clip_expansion(reporter); |
tomhudson@google.com | 381010e | 2013-10-24 11:12:47 +0000 | [diff] [blame] | 1034 | test_hierarchical(reporter); |
scroggo@google.com | d614c6a | 2012-09-14 17:26:37 +0000 | [diff] [blame] | 1035 | } |