| 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 | |
| robertphillips@google.com | 1f2f338 | 2013-08-29 11:54:56 +0000 | [diff] [blame] | 8 | #include "SkBitmapDevice.h" |
| reed@google.com | 21b519d | 2012-10-02 17:42:15 +0000 | [diff] [blame] | 9 | #include "SkCanvas.h" |
| reed@google.com | fe7b1ed | 2012-11-29 21:00:39 +0000 | [diff] [blame] | 10 | #include "SkColorPriv.h" |
| 11 | #include "SkData.h" |
| reed@google.com | bf79023 | 2013-12-13 19:45:58 +0000 | [diff] [blame] | 12 | #include "SkDecodingImageGenerator.h" |
| scroggo@google.com | 49ce11b | 2013-04-25 18:29:32 +0000 | [diff] [blame] | 13 | #include "SkError.h" |
| halcanary@google.com | 3d50ea1 | 2014-01-02 13:15:13 +0000 | [diff] [blame] | 14 | #include "SkImageEncoder.h" |
| 15 | #include "SkImageGenerator.h" |
| reed@google.com | 21b519d | 2012-10-02 17:42:15 +0000 | [diff] [blame] | 16 | #include "SkPaint.h" |
| scroggo@google.com | d614c6a | 2012-09-14 17:26:37 +0000 | [diff] [blame] | 17 | #include "SkPicture.h" |
| robertphillips@google.com | 1f2f338 | 2013-08-29 11:54:56 +0000 | [diff] [blame] | 18 | #include "SkPictureUtils.h" |
| commit-bot@chromium.org | 70512af | 2014-03-18 17:45:32 +0000 | [diff] [blame] | 19 | #include "SkQuadTreePicture.h" |
| reed@google.com | 72aa79c | 2013-01-24 18:27:42 +0000 | [diff] [blame] | 20 | #include "SkRRect.h" |
| tfarina@chromium.org | 8f6884a | 2014-01-24 20:56:26 +0000 | [diff] [blame] | 21 | #include "SkRandom.h" |
| commit-bot@chromium.org | d393b17 | 2014-04-16 16:02:10 +0000 | [diff] [blame] | 22 | #include "SkRTreePicture.h" |
| reed@google.com | fe7b1ed | 2012-11-29 21:00:39 +0000 | [diff] [blame] | 23 | #include "SkShader.h" |
| scroggo@google.com | d614c6a | 2012-09-14 17:26:37 +0000 | [diff] [blame] | 24 | #include "SkStream.h" |
| commit-bot@chromium.org | 70512af | 2014-03-18 17:45:32 +0000 | [diff] [blame] | 25 | #include "SkTileGrid.h" |
| tfarina@chromium.org | 8f6884a | 2014-01-24 20:56:26 +0000 | [diff] [blame] | 26 | #include "Test.h" |
| scroggo@google.com | d614c6a | 2012-09-14 17:26:37 +0000 | [diff] [blame] | 27 | |
| robertphillips@google.com | ed9866c | 2014-01-09 19:20:45 +0000 | [diff] [blame] | 28 | static const int gColorScale = 30; |
| 29 | static const int gColorOffset = 60; |
| reed@google.com | fe7b1ed | 2012-11-29 21:00:39 +0000 | [diff] [blame] | 30 | |
| 31 | 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] | 32 | bm->allocN32Pixels(w, h); |
| reed@google.com | fe7b1ed | 2012-11-29 21:00:39 +0000 | [diff] [blame] | 33 | bm->eraseColor(color); |
| 34 | if (immutable) { |
| 35 | bm->setImmutable(); |
| 36 | } |
| 37 | } |
| 38 | |
| robertphillips@google.com | fe5824a | 2014-01-09 19:45:29 +0000 | [diff] [blame] | 39 | 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] | 40 | SkASSERT(w % 2 == 0); |
| 41 | SkASSERT(h % 2 == 0); |
| mike@reedtribe.org | deee496 | 2014-02-13 14:41:43 +0000 | [diff] [blame] | 42 | bm->allocPixels(SkImageInfo::Make(w, h, kAlpha_8_SkColorType, |
| 43 | kPremul_SkAlphaType)); |
| robertphillips@google.com | ed9866c | 2014-01-09 19:20:45 +0000 | [diff] [blame] | 44 | SkAutoLockPixels lock(*bm); |
| 45 | for (int y = 0; y < h; y += 2) { |
| 46 | uint8_t* s = bm->getAddr8(0, y); |
| 47 | for (int x = 0; x < w; x += 2) { |
| 48 | *s++ = 0xFF; |
| 49 | *s++ = 0x00; |
| 50 | } |
| 51 | s = bm->getAddr8(0, y + 1); |
| 52 | for (int x = 0; x < w; x += 2) { |
| 53 | *s++ = 0x00; |
| 54 | *s++ = 0xFF; |
| 55 | } |
| 56 | } |
| 57 | if (immutable) { |
| 58 | bm->setImmutable(); |
| 59 | } |
| 60 | } |
| reed@google.com | fe7b1ed | 2012-11-29 21:00:39 +0000 | [diff] [blame] | 61 | |
| robertphillips@google.com | ed9866c | 2014-01-09 19:20:45 +0000 | [diff] [blame] | 62 | static void init_paint(SkPaint* paint, const SkBitmap &bm) { |
| 63 | SkShader* shader = SkShader::CreateBitmapShader(bm, |
| 64 | SkShader::kClamp_TileMode, |
| 65 | SkShader::kClamp_TileMode); |
| 66 | paint->setShader(shader)->unref(); |
| 67 | } |
| 68 | |
| skia.committer@gmail.com | 2e9a715 | 2014-01-14 07:01:42 +0000 | [diff] [blame] | 69 | typedef void (*DrawBitmapProc)(SkCanvas*, const SkBitmap&, |
| robertphillips@google.com | 56bf6e4 | 2014-01-13 13:33:26 +0000 | [diff] [blame] | 70 | const SkBitmap&, const SkPoint&, |
| 71 | SkTDArray<SkPixelRef*>* usedPixRefs); |
| robertphillips@google.com | ed9866c | 2014-01-09 19:20:45 +0000 | [diff] [blame] | 72 | |
| skia.committer@gmail.com | 856673a | 2014-01-10 07:08:11 +0000 | [diff] [blame] | 73 | static void drawpaint_proc(SkCanvas* canvas, const SkBitmap& bm, |
| robertphillips@google.com | 56bf6e4 | 2014-01-13 13:33:26 +0000 | [diff] [blame] | 74 | const SkBitmap& altBM, const SkPoint& pos, |
| 75 | SkTDArray<SkPixelRef*>* usedPixRefs) { |
| robertphillips@google.com | ed9866c | 2014-01-09 19:20:45 +0000 | [diff] [blame] | 76 | SkPaint paint; |
| 77 | init_paint(&paint, bm); |
| 78 | |
| 79 | canvas->drawPaint(paint); |
| robertphillips@google.com | 56bf6e4 | 2014-01-13 13:33:26 +0000 | [diff] [blame] | 80 | *usedPixRefs->append() = bm.pixelRef(); |
| robertphillips@google.com | ed9866c | 2014-01-09 19:20:45 +0000 | [diff] [blame] | 81 | } |
| 82 | |
| skia.committer@gmail.com | 856673a | 2014-01-10 07:08:11 +0000 | [diff] [blame] | 83 | static void drawpoints_proc(SkCanvas* canvas, const SkBitmap& bm, |
| robertphillips@google.com | 56bf6e4 | 2014-01-13 13:33:26 +0000 | [diff] [blame] | 84 | const SkBitmap& altBM, const SkPoint& pos, |
| 85 | SkTDArray<SkPixelRef*>* usedPixRefs) { |
| robertphillips@google.com | ed9866c | 2014-01-09 19:20:45 +0000 | [diff] [blame] | 86 | SkPaint paint; |
| 87 | init_paint(&paint, bm); |
| 88 | |
| robertphillips@google.com | 56bf6e4 | 2014-01-13 13:33:26 +0000 | [diff] [blame] | 89 | // draw a rect |
| robertphillips@google.com | ed9866c | 2014-01-09 19:20:45 +0000 | [diff] [blame] | 90 | SkPoint points[5] = { |
| skia.committer@gmail.com | 2e9a715 | 2014-01-14 07:01:42 +0000 | [diff] [blame] | 91 | { pos.fX, pos.fY }, |
| 92 | { pos.fX + bm.width() - 1, pos.fY }, |
| 93 | { pos.fX + bm.width() - 1, pos.fY + bm.height() - 1 }, |
| 94 | { pos.fX, pos.fY + bm.height() - 1 }, |
| 95 | { pos.fX, pos.fY }, |
| robertphillips@google.com | ed9866c | 2014-01-09 19:20:45 +0000 | [diff] [blame] | 96 | }; |
| 97 | |
| 98 | canvas->drawPoints(SkCanvas::kPolygon_PointMode, 5, points, paint); |
| robertphillips@google.com | 56bf6e4 | 2014-01-13 13:33:26 +0000 | [diff] [blame] | 99 | *usedPixRefs->append() = bm.pixelRef(); |
| robertphillips@google.com | ed9866c | 2014-01-09 19:20:45 +0000 | [diff] [blame] | 100 | } |
| 101 | |
| skia.committer@gmail.com | 856673a | 2014-01-10 07:08:11 +0000 | [diff] [blame] | 102 | static void drawrect_proc(SkCanvas* canvas, const SkBitmap& bm, |
| robertphillips@google.com | 56bf6e4 | 2014-01-13 13:33:26 +0000 | [diff] [blame] | 103 | const SkBitmap& altBM, const SkPoint& pos, |
| 104 | SkTDArray<SkPixelRef*>* usedPixRefs) { |
| robertphillips@google.com | ed9866c | 2014-01-09 19:20:45 +0000 | [diff] [blame] | 105 | SkPaint paint; |
| 106 | init_paint(&paint, bm); |
| 107 | |
| 108 | SkRect r = { 0, 0, SkIntToScalar(bm.width()), SkIntToScalar(bm.height()) }; |
| 109 | r.offset(pos.fX, pos.fY); |
| 110 | |
| 111 | canvas->drawRect(r, paint); |
| robertphillips@google.com | 56bf6e4 | 2014-01-13 13:33:26 +0000 | [diff] [blame] | 112 | *usedPixRefs->append() = bm.pixelRef(); |
| robertphillips@google.com | ed9866c | 2014-01-09 19:20:45 +0000 | [diff] [blame] | 113 | } |
| 114 | |
| skia.committer@gmail.com | 856673a | 2014-01-10 07:08:11 +0000 | [diff] [blame] | 115 | static void drawoval_proc(SkCanvas* canvas, const SkBitmap& bm, |
| robertphillips@google.com | 56bf6e4 | 2014-01-13 13:33:26 +0000 | [diff] [blame] | 116 | const SkBitmap& altBM, const SkPoint& pos, |
| 117 | SkTDArray<SkPixelRef*>* usedPixRefs) { |
| robertphillips@google.com | ed9866c | 2014-01-09 19:20:45 +0000 | [diff] [blame] | 118 | SkPaint paint; |
| 119 | init_paint(&paint, bm); |
| 120 | |
| 121 | SkRect r = { 0, 0, SkIntToScalar(bm.width()), SkIntToScalar(bm.height()) }; |
| 122 | r.offset(pos.fX, pos.fY); |
| 123 | |
| 124 | canvas->drawOval(r, paint); |
| robertphillips@google.com | 56bf6e4 | 2014-01-13 13:33:26 +0000 | [diff] [blame] | 125 | *usedPixRefs->append() = bm.pixelRef(); |
| robertphillips@google.com | ed9866c | 2014-01-09 19:20:45 +0000 | [diff] [blame] | 126 | } |
| 127 | |
| skia.committer@gmail.com | 856673a | 2014-01-10 07:08:11 +0000 | [diff] [blame] | 128 | static void drawrrect_proc(SkCanvas* canvas, const SkBitmap& bm, |
| robertphillips@google.com | 56bf6e4 | 2014-01-13 13:33:26 +0000 | [diff] [blame] | 129 | const SkBitmap& altBM, const SkPoint& pos, |
| 130 | SkTDArray<SkPixelRef*>* usedPixRefs) { |
| robertphillips@google.com | ed9866c | 2014-01-09 19:20:45 +0000 | [diff] [blame] | 131 | SkPaint paint; |
| 132 | init_paint(&paint, bm); |
| skia.committer@gmail.com | 856673a | 2014-01-10 07:08:11 +0000 | [diff] [blame] | 133 | |
| robertphillips@google.com | ed9866c | 2014-01-09 19:20:45 +0000 | [diff] [blame] | 134 | SkRect r = { 0, 0, SkIntToScalar(bm.width()), SkIntToScalar(bm.height()) }; |
| 135 | r.offset(pos.fX, pos.fY); |
| 136 | |
| 137 | SkRRect rr; |
| 138 | rr.setRectXY(r, SkIntToScalar(bm.width())/4, SkIntToScalar(bm.height())/4); |
| 139 | canvas->drawRRect(rr, paint); |
| robertphillips@google.com | 56bf6e4 | 2014-01-13 13:33:26 +0000 | [diff] [blame] | 140 | *usedPixRefs->append() = bm.pixelRef(); |
| robertphillips@google.com | ed9866c | 2014-01-09 19:20:45 +0000 | [diff] [blame] | 141 | } |
| 142 | |
| skia.committer@gmail.com | 856673a | 2014-01-10 07:08:11 +0000 | [diff] [blame] | 143 | static void drawpath_proc(SkCanvas* canvas, const SkBitmap& bm, |
| robertphillips@google.com | 56bf6e4 | 2014-01-13 13:33:26 +0000 | [diff] [blame] | 144 | const SkBitmap& altBM, const SkPoint& pos, |
| 145 | SkTDArray<SkPixelRef*>* usedPixRefs) { |
| robertphillips@google.com | ed9866c | 2014-01-09 19:20:45 +0000 | [diff] [blame] | 146 | SkPaint paint; |
| 147 | init_paint(&paint, bm); |
| 148 | |
| 149 | SkPath path; |
| 150 | path.lineTo(bm.width()/2.0f, SkIntToScalar(bm.height())); |
| 151 | path.lineTo(SkIntToScalar(bm.width()), 0); |
| 152 | path.close(); |
| 153 | path.offset(pos.fX, pos.fY); |
| 154 | |
| 155 | canvas->drawPath(path, paint); |
| robertphillips@google.com | 56bf6e4 | 2014-01-13 13:33:26 +0000 | [diff] [blame] | 156 | *usedPixRefs->append() = bm.pixelRef(); |
| robertphillips@google.com | ed9866c | 2014-01-09 19:20:45 +0000 | [diff] [blame] | 157 | } |
| 158 | |
| skia.committer@gmail.com | 856673a | 2014-01-10 07:08:11 +0000 | [diff] [blame] | 159 | static void drawbitmap_proc(SkCanvas* canvas, const SkBitmap& bm, |
| robertphillips@google.com | 56bf6e4 | 2014-01-13 13:33:26 +0000 | [diff] [blame] | 160 | const SkBitmap& altBM, const SkPoint& pos, |
| 161 | SkTDArray<SkPixelRef*>* usedPixRefs) { |
| reed@google.com | fe7b1ed | 2012-11-29 21:00:39 +0000 | [diff] [blame] | 162 | canvas->drawBitmap(bm, pos.fX, pos.fY, NULL); |
| robertphillips@google.com | 56bf6e4 | 2014-01-13 13:33:26 +0000 | [diff] [blame] | 163 | *usedPixRefs->append() = bm.pixelRef(); |
| reed@google.com | fe7b1ed | 2012-11-29 21:00:39 +0000 | [diff] [blame] | 164 | } |
| 165 | |
| skia.committer@gmail.com | 856673a | 2014-01-10 07:08:11 +0000 | [diff] [blame] | 166 | static void drawbitmap_withshader_proc(SkCanvas* canvas, const SkBitmap& bm, |
| robertphillips@google.com | 56bf6e4 | 2014-01-13 13:33:26 +0000 | [diff] [blame] | 167 | const SkBitmap& altBM, const SkPoint& pos, |
| 168 | SkTDArray<SkPixelRef*>* usedPixRefs) { |
| robertphillips@google.com | ed9866c | 2014-01-09 19:20:45 +0000 | [diff] [blame] | 169 | SkPaint paint; |
| 170 | init_paint(&paint, bm); |
| 171 | |
| 172 | // The bitmap in the paint is ignored unless we're drawing an A8 bitmap |
| 173 | canvas->drawBitmap(altBM, pos.fX, pos.fY, &paint); |
| robertphillips@google.com | 56bf6e4 | 2014-01-13 13:33:26 +0000 | [diff] [blame] | 174 | *usedPixRefs->append() = bm.pixelRef(); |
| 175 | *usedPixRefs->append() = altBM.pixelRef(); |
| robertphillips@google.com | ed9866c | 2014-01-09 19:20:45 +0000 | [diff] [blame] | 176 | } |
| 177 | |
| skia.committer@gmail.com | 856673a | 2014-01-10 07:08:11 +0000 | [diff] [blame] | 178 | static void drawsprite_proc(SkCanvas* canvas, const SkBitmap& bm, |
| robertphillips@google.com | 56bf6e4 | 2014-01-13 13:33:26 +0000 | [diff] [blame] | 179 | const SkBitmap& altBM, const SkPoint& pos, |
| 180 | SkTDArray<SkPixelRef*>* usedPixRefs) { |
| robertphillips@google.com | ed9866c | 2014-01-09 19:20:45 +0000 | [diff] [blame] | 181 | const SkMatrix& ctm = canvas->getTotalMatrix(); |
| 182 | |
| 183 | SkPoint p(pos); |
| 184 | ctm.mapPoints(&p, 1); |
| 185 | |
| 186 | canvas->drawSprite(bm, (int)p.fX, (int)p.fY, NULL); |
| robertphillips@google.com | 56bf6e4 | 2014-01-13 13:33:26 +0000 | [diff] [blame] | 187 | *usedPixRefs->append() = bm.pixelRef(); |
| robertphillips@google.com | ed9866c | 2014-01-09 19:20:45 +0000 | [diff] [blame] | 188 | } |
| 189 | |
| 190 | #if 0 |
| 191 | // Although specifiable, this case doesn't seem to make sense (i.e., the |
| 192 | // bitmap in the shader is never used). |
| skia.committer@gmail.com | 856673a | 2014-01-10 07:08:11 +0000 | [diff] [blame] | 193 | static void drawsprite_withshader_proc(SkCanvas* canvas, const SkBitmap& bm, |
| robertphillips@google.com | 56bf6e4 | 2014-01-13 13:33:26 +0000 | [diff] [blame] | 194 | const SkBitmap& altBM, const SkPoint& pos, |
| 195 | SkTDArray<SkPixelRef*>* usedPixRefs) { |
| robertphillips@google.com | ed9866c | 2014-01-09 19:20:45 +0000 | [diff] [blame] | 196 | SkPaint paint; |
| 197 | init_paint(&paint, bm); |
| 198 | |
| 199 | const SkMatrix& ctm = canvas->getTotalMatrix(); |
| 200 | |
| 201 | SkPoint p(pos); |
| 202 | ctm.mapPoints(&p, 1); |
| 203 | |
| 204 | canvas->drawSprite(altBM, (int)p.fX, (int)p.fY, &paint); |
| robertphillips@google.com | 56bf6e4 | 2014-01-13 13:33:26 +0000 | [diff] [blame] | 205 | *usedPixRefs->append() = bm.pixelRef(); |
| 206 | *usedPixRefs->append() = altBM.pixelRef(); |
| robertphillips@google.com | ed9866c | 2014-01-09 19:20:45 +0000 | [diff] [blame] | 207 | } |
| 208 | #endif |
| 209 | |
| skia.committer@gmail.com | 856673a | 2014-01-10 07:08:11 +0000 | [diff] [blame] | 210 | static void drawbitmaprect_proc(SkCanvas* canvas, const SkBitmap& bm, |
| robertphillips@google.com | 56bf6e4 | 2014-01-13 13:33:26 +0000 | [diff] [blame] | 211 | const SkBitmap& altBM, const SkPoint& pos, |
| 212 | SkTDArray<SkPixelRef*>* usedPixRefs) { |
| robertphillips@google.com | ed9866c | 2014-01-09 19:20:45 +0000 | [diff] [blame] | 213 | SkRect r = { 0, 0, SkIntToScalar(bm.width()), SkIntToScalar(bm.height()) }; |
| 214 | |
| reed@google.com | fe7b1ed | 2012-11-29 21:00:39 +0000 | [diff] [blame] | 215 | r.offset(pos.fX, pos.fY); |
| 216 | canvas->drawBitmapRectToRect(bm, NULL, r, NULL); |
| robertphillips@google.com | 56bf6e4 | 2014-01-13 13:33:26 +0000 | [diff] [blame] | 217 | *usedPixRefs->append() = bm.pixelRef(); |
| reed@google.com | fe7b1ed | 2012-11-29 21:00:39 +0000 | [diff] [blame] | 218 | } |
| 219 | |
| skia.committer@gmail.com | 856673a | 2014-01-10 07:08:11 +0000 | [diff] [blame] | 220 | static void drawbitmaprect_withshader_proc(SkCanvas* canvas, |
| 221 | const SkBitmap& bm, |
| 222 | const SkBitmap& altBM, |
| robertphillips@google.com | 56bf6e4 | 2014-01-13 13:33:26 +0000 | [diff] [blame] | 223 | const SkPoint& pos, |
| 224 | SkTDArray<SkPixelRef*>* usedPixRefs) { |
| robertphillips@google.com | ed9866c | 2014-01-09 19:20:45 +0000 | [diff] [blame] | 225 | SkPaint paint; |
| 226 | init_paint(&paint, bm); |
| 227 | |
| 228 | SkRect r = { 0, 0, SkIntToScalar(bm.width()), SkIntToScalar(bm.height()) }; |
| reed@google.com | fe7b1ed | 2012-11-29 21:00:39 +0000 | [diff] [blame] | 229 | r.offset(pos.fX, pos.fY); |
| 230 | |
| robertphillips@google.com | ed9866c | 2014-01-09 19:20:45 +0000 | [diff] [blame] | 231 | // The bitmap in the paint is ignored unless we're drawing an A8 bitmap |
| 232 | canvas->drawBitmapRectToRect(altBM, NULL, r, &paint); |
| robertphillips@google.com | 56bf6e4 | 2014-01-13 13:33:26 +0000 | [diff] [blame] | 233 | *usedPixRefs->append() = bm.pixelRef(); |
| 234 | *usedPixRefs->append() = altBM.pixelRef(); |
| robertphillips@google.com | ed9866c | 2014-01-09 19:20:45 +0000 | [diff] [blame] | 235 | } |
| 236 | |
| skia.committer@gmail.com | 856673a | 2014-01-10 07:08:11 +0000 | [diff] [blame] | 237 | static void drawtext_proc(SkCanvas* canvas, const SkBitmap& bm, |
| robertphillips@google.com | 56bf6e4 | 2014-01-13 13:33:26 +0000 | [diff] [blame] | 238 | const SkBitmap& altBM, const SkPoint& pos, |
| 239 | SkTDArray<SkPixelRef*>* usedPixRefs) { |
| reed@google.com | fe7b1ed | 2012-11-29 21:00:39 +0000 | [diff] [blame] | 240 | SkPaint paint; |
| robertphillips@google.com | ed9866c | 2014-01-09 19:20:45 +0000 | [diff] [blame] | 241 | init_paint(&paint, bm); |
| 242 | paint.setTextSize(SkIntToScalar(1.5*bm.width())); |
| 243 | |
| 244 | canvas->drawText("0", 1, pos.fX, pos.fY+bm.width(), paint); |
| robertphillips@google.com | 56bf6e4 | 2014-01-13 13:33:26 +0000 | [diff] [blame] | 245 | *usedPixRefs->append() = bm.pixelRef(); |
| robertphillips@google.com | ed9866c | 2014-01-09 19:20:45 +0000 | [diff] [blame] | 246 | } |
| 247 | |
| skia.committer@gmail.com | 856673a | 2014-01-10 07:08:11 +0000 | [diff] [blame] | 248 | static void drawpostext_proc(SkCanvas* canvas, const SkBitmap& bm, |
| robertphillips@google.com | 56bf6e4 | 2014-01-13 13:33:26 +0000 | [diff] [blame] | 249 | const SkBitmap& altBM, const SkPoint& pos, |
| 250 | SkTDArray<SkPixelRef*>* usedPixRefs) { |
| robertphillips@google.com | ed9866c | 2014-01-09 19:20:45 +0000 | [diff] [blame] | 251 | SkPaint paint; |
| 252 | init_paint(&paint, bm); |
| 253 | paint.setTextSize(SkIntToScalar(1.5*bm.width())); |
| 254 | |
| 255 | SkPoint point = { pos.fX, pos.fY + bm.height() }; |
| 256 | canvas->drawPosText("O", 1, &point, paint); |
| robertphillips@google.com | 56bf6e4 | 2014-01-13 13:33:26 +0000 | [diff] [blame] | 257 | *usedPixRefs->append() = bm.pixelRef(); |
| robertphillips@google.com | ed9866c | 2014-01-09 19:20:45 +0000 | [diff] [blame] | 258 | } |
| 259 | |
| skia.committer@gmail.com | 856673a | 2014-01-10 07:08:11 +0000 | [diff] [blame] | 260 | static void drawtextonpath_proc(SkCanvas* canvas, const SkBitmap& bm, |
| robertphillips@google.com | 56bf6e4 | 2014-01-13 13:33:26 +0000 | [diff] [blame] | 261 | const SkBitmap& altBM, const SkPoint& pos, |
| 262 | SkTDArray<SkPixelRef*>* usedPixRefs) { |
| robertphillips@google.com | ed9866c | 2014-01-09 19:20:45 +0000 | [diff] [blame] | 263 | SkPaint paint; |
| 264 | |
| 265 | init_paint(&paint, bm); |
| 266 | paint.setTextSize(SkIntToScalar(1.5*bm.width())); |
| 267 | |
| 268 | SkPath path; |
| 269 | path.lineTo(SkIntToScalar(bm.width()), 0); |
| 270 | path.offset(pos.fX, pos.fY+bm.height()); |
| 271 | |
| 272 | canvas->drawTextOnPath("O", 1, path, NULL, paint); |
| robertphillips@google.com | 56bf6e4 | 2014-01-13 13:33:26 +0000 | [diff] [blame] | 273 | *usedPixRefs->append() = bm.pixelRef(); |
| robertphillips@google.com | ed9866c | 2014-01-09 19:20:45 +0000 | [diff] [blame] | 274 | } |
| 275 | |
| skia.committer@gmail.com | 856673a | 2014-01-10 07:08:11 +0000 | [diff] [blame] | 276 | static void drawverts_proc(SkCanvas* canvas, const SkBitmap& bm, |
| robertphillips@google.com | 56bf6e4 | 2014-01-13 13:33:26 +0000 | [diff] [blame] | 277 | const SkBitmap& altBM, const SkPoint& pos, |
| 278 | SkTDArray<SkPixelRef*>* usedPixRefs) { |
| robertphillips@google.com | ed9866c | 2014-01-09 19:20:45 +0000 | [diff] [blame] | 279 | SkPaint paint; |
| 280 | init_paint(&paint, bm); |
| 281 | |
| skia.committer@gmail.com | 2e9a715 | 2014-01-14 07:01:42 +0000 | [diff] [blame] | 282 | SkPoint verts[4] = { |
| 283 | { pos.fX, pos.fY }, |
| 284 | { pos.fX + bm.width(), pos.fY }, |
| 285 | { pos.fX + bm.width(), pos.fY + bm.height() }, |
| 286 | { pos.fX, pos.fY + bm.height() } |
| robertphillips@google.com | ed9866c | 2014-01-09 19:20:45 +0000 | [diff] [blame] | 287 | }; |
| skia.committer@gmail.com | 856673a | 2014-01-10 07:08:11 +0000 | [diff] [blame] | 288 | SkPoint texs[4] = { { 0, 0 }, |
| 289 | { SkIntToScalar(bm.width()), 0 }, |
| 290 | { SkIntToScalar(bm.width()), SkIntToScalar(bm.height()) }, |
| robertphillips@google.com | ed9866c | 2014-01-09 19:20:45 +0000 | [diff] [blame] | 291 | { 0, SkIntToScalar(bm.height()) } }; |
| 292 | uint16_t indices[6] = { 0, 1, 2, 0, 2, 3 }; |
| 293 | |
| skia.committer@gmail.com | 856673a | 2014-01-10 07:08:11 +0000 | [diff] [blame] | 294 | canvas->drawVertices(SkCanvas::kTriangles_VertexMode, 4, verts, texs, NULL, NULL, |
| robertphillips@google.com | ed9866c | 2014-01-09 19:20:45 +0000 | [diff] [blame] | 295 | indices, 6, paint); |
| robertphillips@google.com | 56bf6e4 | 2014-01-13 13:33:26 +0000 | [diff] [blame] | 296 | *usedPixRefs->append() = bm.pixelRef(); |
| reed@google.com | fe7b1ed | 2012-11-29 21:00:39 +0000 | [diff] [blame] | 297 | } |
| 298 | |
| 299 | // 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] | 300 | static SkPicture* record_bitmaps(const SkBitmap bm[], |
| robertphillips@google.com | 56bf6e4 | 2014-01-13 13:33:26 +0000 | [diff] [blame] | 301 | const SkPoint pos[], |
| 302 | SkTDArray<SkPixelRef*> analytic[], |
| skia.committer@gmail.com | 2e9a715 | 2014-01-14 07:01:42 +0000 | [diff] [blame] | 303 | int count, |
| robertphillips@google.com | 56bf6e4 | 2014-01-13 13:33:26 +0000 | [diff] [blame] | 304 | DrawBitmapProc proc) { |
| robertphillips@google.com | 84b18c7 | 2014-04-13 19:09:42 +0000 | [diff] [blame] | 305 | SkPictureRecorder recorder; |
| commit-bot@chromium.org | 5fb2ce3 | 2014-04-17 23:35:06 +0000 | [diff] [blame^] | 306 | SkCanvas* canvas = recorder.beginRecording(1000, 1000, NULL, 0); |
| reed@google.com | fe7b1ed | 2012-11-29 21:00:39 +0000 | [diff] [blame] | 307 | for (int i = 0; i < count; ++i) { |
| robertphillips@google.com | 56bf6e4 | 2014-01-13 13:33:26 +0000 | [diff] [blame] | 308 | analytic[i].rewind(); |
| robertphillips@google.com | ed9866c | 2014-01-09 19:20:45 +0000 | [diff] [blame] | 309 | canvas->save(); |
| 310 | SkRect clipRect = SkRect::MakeXYWH(pos[i].fX, pos[i].fY, |
| 311 | SkIntToScalar(bm[i].width()), |
| 312 | SkIntToScalar(bm[i].height())); |
| 313 | canvas->clipRect(clipRect, SkRegion::kIntersect_Op); |
| robertphillips@google.com | 56bf6e4 | 2014-01-13 13:33:26 +0000 | [diff] [blame] | 314 | proc(canvas, bm[i], bm[count+i], pos[i], &analytic[i]); |
| robertphillips@google.com | ed9866c | 2014-01-09 19:20:45 +0000 | [diff] [blame] | 315 | canvas->restore(); |
| reed@google.com | fe7b1ed | 2012-11-29 21:00:39 +0000 | [diff] [blame] | 316 | } |
| robertphillips@google.com | 84b18c7 | 2014-04-13 19:09:42 +0000 | [diff] [blame] | 317 | return recorder.endRecording(); |
| reed@google.com | fe7b1ed | 2012-11-29 21:00:39 +0000 | [diff] [blame] | 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 | |
| commit-bot@chromium.org | 5bee054 | 2014-03-05 20:59:45 +0000 | [diff] [blame] | 421 | |
| 422 | static const struct { |
| 423 | const DrawBitmapProc proc; |
| 424 | const char* const desc; |
| 425 | } gProcs[] = { |
| 426 | {drawpaint_proc, "drawpaint"}, |
| 427 | {drawpoints_proc, "drawpoints"}, |
| 428 | {drawrect_proc, "drawrect"}, |
| 429 | {drawoval_proc, "drawoval"}, |
| 430 | {drawrrect_proc, "drawrrect"}, |
| 431 | {drawpath_proc, "drawpath"}, |
| 432 | {drawbitmap_proc, "drawbitmap"}, |
| 433 | {drawbitmap_withshader_proc, "drawbitmap_withshader"}, |
| 434 | {drawsprite_proc, "drawsprite"}, |
| robertphillips@google.com | 56bf6e4 | 2014-01-13 13:33:26 +0000 | [diff] [blame] | 435 | #if 0 |
| commit-bot@chromium.org | 5bee054 | 2014-03-05 20:59:45 +0000 | [diff] [blame] | 436 | {drawsprite_withshader_proc, "drawsprite_withshader"}, |
| robertphillips@google.com | 56bf6e4 | 2014-01-13 13:33:26 +0000 | [diff] [blame] | 437 | #endif |
| commit-bot@chromium.org | 5bee054 | 2014-03-05 20:59:45 +0000 | [diff] [blame] | 438 | {drawbitmaprect_proc, "drawbitmaprect"}, |
| 439 | {drawbitmaprect_withshader_proc, "drawbitmaprect_withshader"}, |
| 440 | {drawtext_proc, "drawtext"}, |
| 441 | {drawpostext_proc, "drawpostext"}, |
| 442 | {drawtextonpath_proc, "drawtextonpath"}, |
| 443 | {drawverts_proc, "drawverts"}, |
| robertphillips@google.com | 56bf6e4 | 2014-01-13 13:33:26 +0000 | [diff] [blame] | 444 | }; |
| 445 | |
| 446 | 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] | 447 | // Our convention is that the color components contain an encoding of |
| 448 | // 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] | 449 | // reserved for the background |
| 450 | for (int i = 0; i < num; ++i) { |
| skia.committer@gmail.com | 2e9a715 | 2014-01-14 07:01:42 +0000 | [diff] [blame] | 451 | make_bm(&bm[i], w, h, |
| 452 | SkColorSetARGB(0xFF, |
| 453 | gColorScale*i+gColorOffset, |
| 454 | gColorScale*i+gColorOffset, |
| 455 | gColorScale*i+gColorOffset), |
| robertphillips@google.com | 56bf6e4 | 2014-01-13 13:33:26 +0000 | [diff] [blame] | 456 | true); |
| 457 | refs[i] = bm[i].pixelRef(); |
| 458 | } |
| 459 | |
| 460 | // The A8 alternate bitmaps are all BW checkerboards |
| 461 | for (int i = 0; i < num; ++i) { |
| 462 | make_checkerboard(&bm[num+i], w, h, true); |
| 463 | refs[num+i] = bm[num+i].pixelRef(); |
| 464 | } |
| 465 | } |
| 466 | |
| reed@google.com | fe7b1ed | 2012-11-29 21:00:39 +0000 | [diff] [blame] | 467 | static void test_gatherpixelrefs(skiatest::Reporter* reporter) { |
| robertphillips@google.com | ed9866c | 2014-01-09 19:20:45 +0000 | [diff] [blame] | 468 | const int IW = 32; |
| reed@google.com | fe7b1ed | 2012-11-29 21:00:39 +0000 | [diff] [blame] | 469 | const int IH = IW; |
| 470 | const SkScalar W = SkIntToScalar(IW); |
| 471 | const SkScalar H = W; |
| 472 | |
| 473 | static const int N = 4; |
| robertphillips@google.com | ed9866c | 2014-01-09 19:20:45 +0000 | [diff] [blame] | 474 | SkBitmap bm[2*N]; |
| 475 | SkPixelRef* refs[2*N]; |
| robertphillips@google.com | 56bf6e4 | 2014-01-13 13:33:26 +0000 | [diff] [blame] | 476 | SkTDArray<SkPixelRef*> analytic[N]; |
| reed@google.com | fe7b1ed | 2012-11-29 21:00:39 +0000 | [diff] [blame] | 477 | |
| robertphillips@google.com | ed9866c | 2014-01-09 19:20:45 +0000 | [diff] [blame] | 478 | const SkPoint pos[N] = { |
| reed@google.com | fe7b1ed | 2012-11-29 21:00:39 +0000 | [diff] [blame] | 479 | { 0, 0 }, { W, 0 }, { 0, H }, { W, H } |
| 480 | }; |
| 481 | |
| robertphillips@google.com | 56bf6e4 | 2014-01-13 13:33:26 +0000 | [diff] [blame] | 482 | create_textures(bm, refs, N, IW, IH); |
| reed@google.com | fe7b1ed | 2012-11-29 21:00:39 +0000 | [diff] [blame] | 483 | |
| commit-bot@chromium.org | e0e7cfe | 2013-09-09 20:09:12 +0000 | [diff] [blame] | 484 | SkRandom rand; |
| robertphillips@google.com | 56bf6e4 | 2014-01-13 13:33:26 +0000 | [diff] [blame] | 485 | 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] | 486 | SkAutoTUnref<SkPicture> pic( |
| 487 | record_bitmaps(bm, pos, analytic, N, gProcs[k].proc)); |
| reed@google.com | fe7b1ed | 2012-11-29 21:00:39 +0000 | [diff] [blame] | 488 | |
| tomhudson@google.com | 381010e | 2013-10-24 11:12:47 +0000 | [diff] [blame] | 489 | REPORTER_ASSERT(reporter, pic->willPlayBackBitmaps() || N == 0); |
| reed@google.com | fe7b1ed | 2012-11-29 21:00:39 +0000 | [diff] [blame] | 490 | // 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] | 491 | // contain 1 or 2 bitmaps. |
| reed@google.com | fe7b1ed | 2012-11-29 21:00:39 +0000 | [diff] [blame] | 492 | for (size_t i = 0; i < SK_ARRAY_COUNT(pos); ++i) { |
| 493 | SkRect r; |
| 494 | r.set(2, 2, W - 2, H - 2); |
| 495 | r.offset(pos[i].fX, pos[i].fY); |
| 496 | SkAutoDataUnref data(SkPictureUtils::GatherPixelRefs(pic, r)); |
| commit-bot@chromium.org | 5bee054 | 2014-03-05 20:59:45 +0000 | [diff] [blame] | 497 | if (!data) { |
| 498 | ERRORF(reporter, "SkPictureUtils::GatherPixelRefs returned " |
| 499 | "NULL for %s.", gProcs[k].desc); |
| 500 | continue; |
| 501 | } |
| 502 | SkPixelRef** gatheredRefs = (SkPixelRef**)data->data(); |
| 503 | int count = static_cast<int>(data->size() / sizeof(SkPixelRef*)); |
| 504 | REPORTER_ASSERT(reporter, 1 == count || 2 == count); |
| 505 | if (1 == count) { |
| 506 | REPORTER_ASSERT(reporter, gatheredRefs[0] == refs[i]); |
| 507 | } else if (2 == count) { |
| 508 | REPORTER_ASSERT(reporter, |
| 509 | (gatheredRefs[0] == refs[i] && gatheredRefs[1] == refs[i+N]) || |
| 510 | (gatheredRefs[1] == refs[i] && gatheredRefs[0] == refs[i+N])); |
| commit-bot@chromium.org | fe433c1 | 2013-07-09 16:04:32 +0000 | [diff] [blame] | 511 | } |
| reed@google.com | fe7b1ed | 2012-11-29 21:00:39 +0000 | [diff] [blame] | 512 | } |
| 513 | |
| robertphillips@google.com | ed9866c | 2014-01-09 19:20:45 +0000 | [diff] [blame] | 514 | SkBitmap image; |
| 515 | draw(pic, 2*IW, 2*IH, &image); |
| 516 | |
| reed@google.com | fe7b1ed | 2012-11-29 21:00:39 +0000 | [diff] [blame] | 517 | // Test a bunch of random (mostly) rects, and compare the gather results |
| 518 | // with a deduced list of refs by looking at the colors drawn. |
| 519 | for (int j = 0; j < 100; ++j) { |
| 520 | SkRect r; |
| 521 | rand_rect(&r, rand, 2*W, 2*H); |
| 522 | |
| robertphillips@google.com | 56bf6e4 | 2014-01-13 13:33:26 +0000 | [diff] [blame] | 523 | SkTDArray<SkPixelRef*> fromImage; |
| 524 | gather_from_image(image, refs, N, &fromImage, r); |
| 525 | |
| 526 | SkTDArray<SkPixelRef*> fromAnalytic; |
| 527 | gather_from_analytic(pos, W, H, analytic, N, &fromAnalytic, r); |
| reed@google.com | fe7b1ed | 2012-11-29 21:00:39 +0000 | [diff] [blame] | 528 | |
| 529 | SkData* data = SkPictureUtils::GatherPixelRefs(pic, r); |
| 530 | size_t dataSize = data ? data->size() : 0; |
| robertphillips@google.com | e9cd27d | 2013-10-16 17:48:11 +0000 | [diff] [blame] | 531 | int gatherCount = static_cast<int>(dataSize / sizeof(SkPixelRef*)); |
| reed@google.com | fe7b1ed | 2012-11-29 21:00:39 +0000 | [diff] [blame] | 532 | SkASSERT(gatherCount * sizeof(SkPixelRef*) == dataSize); |
| 533 | SkPixelRef** gatherRefs = data ? (SkPixelRef**)(data->data()) : NULL; |
| 534 | SkAutoDataUnref adu(data); |
| 535 | |
| robertphillips@google.com | 56bf6e4 | 2014-01-13 13:33:26 +0000 | [diff] [blame] | 536 | // Everything that we saw drawn should appear in the analytic list |
| 537 | // but the analytic list may contain some pixelRefs that were not |
| 538 | // seen in the image (e.g., A8 textures used as masks) |
| 539 | for (int i = 0; i < fromImage.count(); ++i) { |
| commit-bot@chromium.org | 5bee054 | 2014-03-05 20:59:45 +0000 | [diff] [blame] | 540 | if (-1 == fromAnalytic.find(fromImage[i])) { |
| 541 | ERRORF(reporter, "PixelRef missing %d %s", |
| 542 | i, gProcs[k].desc); |
| 543 | } |
| robertphillips@google.com | 56bf6e4 | 2014-01-13 13:33:26 +0000 | [diff] [blame] | 544 | } |
| skia.committer@gmail.com | c3d7d90 | 2012-11-30 02:01:24 +0000 | [diff] [blame] | 545 | |
| reed@google.com | fe7b1ed | 2012-11-29 21:00:39 +0000 | [diff] [blame] | 546 | /* |
| 547 | * GatherPixelRefs is conservative, so it can return more bitmaps |
| robertphillips@google.com | 56bf6e4 | 2014-01-13 13:33:26 +0000 | [diff] [blame] | 548 | * than are strictly required. Thus our check here is only that |
| 549 | * 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] | 550 | * a strict requirement on Gather, which is meant to be quick and |
| 551 | * only mostly-correct, but at the moment this test should work. |
| 552 | */ |
| robertphillips@google.com | 56bf6e4 | 2014-01-13 13:33:26 +0000 | [diff] [blame] | 553 | for (int i = 0; i < fromAnalytic.count(); ++i) { |
| 554 | bool found = find(gatherRefs, fromAnalytic[i], gatherCount); |
| commit-bot@chromium.org | 5bee054 | 2014-03-05 20:59:45 +0000 | [diff] [blame] | 555 | if (!found) { |
| 556 | ERRORF(reporter, "PixelRef missing %d %s", |
| 557 | i, gProcs[k].desc); |
| 558 | } |
| reed@google.com | fe7b1ed | 2012-11-29 21:00:39 +0000 | [diff] [blame] | 559 | #if 0 |
| 560 | // enable this block of code to debug failures, as it will rerun |
| 561 | // the case that failed. |
| 562 | if (!found) { |
| 563 | SkData* data = SkPictureUtils::GatherPixelRefs(pic, r); |
| 564 | size_t dataSize = data ? data->size() : 0; |
| 565 | } |
| 566 | #endif |
| 567 | } |
| 568 | } |
| 569 | } |
| 570 | } |
| 571 | |
| robertphillips@google.com | 56bf6e4 | 2014-01-13 13:33:26 +0000 | [diff] [blame] | 572 | static void test_gatherpixelrefsandrects(skiatest::Reporter* reporter) { |
| 573 | const int IW = 32; |
| 574 | const int IH = IW; |
| 575 | const SkScalar W = SkIntToScalar(IW); |
| 576 | const SkScalar H = W; |
| 577 | |
| 578 | static const int N = 4; |
| 579 | SkBitmap bm[2*N]; |
| 580 | SkPixelRef* refs[2*N]; |
| 581 | SkTDArray<SkPixelRef*> analytic[N]; |
| 582 | |
| 583 | const SkPoint pos[N] = { |
| 584 | { 0, 0 }, { W, 0 }, { 0, H }, { W, H } |
| 585 | }; |
| 586 | |
| 587 | create_textures(bm, refs, N, IW, IH); |
| 588 | |
| 589 | SkRandom rand; |
| 590 | 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] | 591 | SkAutoTUnref<SkPicture> pic( |
| 592 | record_bitmaps(bm, pos, analytic, N, gProcs[k].proc)); |
| robertphillips@google.com | 56bf6e4 | 2014-01-13 13:33:26 +0000 | [diff] [blame] | 593 | |
| 594 | REPORTER_ASSERT(reporter, pic->willPlayBackBitmaps() || N == 0); |
| 595 | |
| 596 | SkAutoTUnref<SkPictureUtils::SkPixelRefContainer> prCont( |
| 597 | new SkPictureUtils::SkPixelRefsAndRectsList); |
| 598 | |
| 599 | SkPictureUtils::GatherPixelRefsAndRects(pic, prCont); |
| 600 | |
| 601 | // quick check for a small piece of each quadrant, which should just |
| 602 | // contain 1 or 2 bitmaps. |
| 603 | for (size_t i = 0; i < SK_ARRAY_COUNT(pos); ++i) { |
| 604 | SkRect r; |
| 605 | r.set(2, 2, W - 2, H - 2); |
| 606 | r.offset(pos[i].fX, pos[i].fY); |
| 607 | |
| 608 | SkTDArray<SkPixelRef*> gatheredRefs; |
| 609 | prCont->query(r, &gatheredRefs); |
| 610 | |
| 611 | int count = gatheredRefs.count(); |
| 612 | REPORTER_ASSERT(reporter, 1 == count || 2 == count); |
| 613 | if (1 == count) { |
| 614 | REPORTER_ASSERT(reporter, gatheredRefs[0] == refs[i]); |
| 615 | } else if (2 == count) { |
| skia.committer@gmail.com | 2e9a715 | 2014-01-14 07:01:42 +0000 | [diff] [blame] | 616 | REPORTER_ASSERT(reporter, |
| robertphillips@google.com | 56bf6e4 | 2014-01-13 13:33:26 +0000 | [diff] [blame] | 617 | (gatheredRefs[0] == refs[i] && gatheredRefs[1] == refs[i+N]) || |
| 618 | (gatheredRefs[1] == refs[i] && gatheredRefs[0] == refs[i+N])); |
| 619 | } |
| 620 | } |
| 621 | |
| 622 | SkBitmap image; |
| 623 | draw(pic, 2*IW, 2*IH, &image); |
| 624 | |
| 625 | // Test a bunch of random (mostly) rects, and compare the gather results |
| 626 | // with the analytic results and the pixel refs seen in a rendering. |
| 627 | for (int j = 0; j < 100; ++j) { |
| 628 | SkRect r; |
| 629 | rand_rect(&r, rand, 2*W, 2*H); |
| 630 | |
| 631 | SkTDArray<SkPixelRef*> fromImage; |
| 632 | gather_from_image(image, refs, N, &fromImage, r); |
| 633 | |
| 634 | SkTDArray<SkPixelRef*> fromAnalytic; |
| 635 | gather_from_analytic(pos, W, H, analytic, N, &fromAnalytic, r); |
| 636 | |
| 637 | SkTDArray<SkPixelRef*> gatheredRefs; |
| 638 | prCont->query(r, &gatheredRefs); |
| 639 | |
| 640 | // Everything that we saw drawn should appear in the analytic list |
| 641 | // but the analytic list may contain some pixelRefs that were not |
| 642 | // seen in the image (e.g., A8 textures used as masks) |
| 643 | for (int i = 0; i < fromImage.count(); ++i) { |
| 644 | REPORTER_ASSERT(reporter, -1 != fromAnalytic.find(fromImage[i])); |
| 645 | } |
| 646 | |
| 647 | // Everything in the analytic list should appear in the gathered |
| skia.committer@gmail.com | 2e9a715 | 2014-01-14 07:01:42 +0000 | [diff] [blame] | 648 | // list. |
| robertphillips@google.com | 56bf6e4 | 2014-01-13 13:33:26 +0000 | [diff] [blame] | 649 | for (int i = 0; i < fromAnalytic.count(); ++i) { |
| 650 | REPORTER_ASSERT(reporter, -1 != gatheredRefs.find(fromAnalytic[i])); |
| 651 | } |
| 652 | } |
| 653 | } |
| 654 | } |
| 655 | |
| scroggo@google.com | d614c6a | 2012-09-14 17:26:37 +0000 | [diff] [blame] | 656 | #ifdef SK_DEBUG |
| 657 | // Ensure that deleting SkPicturePlayback does not assert. Asserts only fire in debug mode, so only |
| 658 | // run in debug mode. |
| 659 | static void test_deleting_empty_playback() { |
| robertphillips@google.com | 84b18c7 | 2014-04-13 19:09:42 +0000 | [diff] [blame] | 660 | SkPictureRecorder recorder; |
| scroggo@google.com | d614c6a | 2012-09-14 17:26:37 +0000 | [diff] [blame] | 661 | // Creates an SkPictureRecord |
| commit-bot@chromium.org | 5fb2ce3 | 2014-04-17 23:35:06 +0000 | [diff] [blame^] | 662 | recorder.beginRecording(0, 0, NULL, 0); |
| scroggo@google.com | d614c6a | 2012-09-14 17:26:37 +0000 | [diff] [blame] | 663 | // Turns that into an SkPicturePlayback |
| robertphillips@google.com | 84b18c7 | 2014-04-13 19:09:42 +0000 | [diff] [blame] | 664 | SkAutoTUnref<SkPicture> picture(recorder.endRecording()); |
| scroggo@google.com | d614c6a | 2012-09-14 17:26:37 +0000 | [diff] [blame] | 665 | // Deletes the old SkPicturePlayback, and creates a new SkPictureRecord |
| commit-bot@chromium.org | 5fb2ce3 | 2014-04-17 23:35:06 +0000 | [diff] [blame^] | 666 | recorder.beginRecording(0, 0, NULL, 0); |
| scroggo@google.com | d614c6a | 2012-09-14 17:26:37 +0000 | [diff] [blame] | 667 | } |
| 668 | |
| 669 | // Ensure that serializing an empty picture does not assert. Likewise only runs in debug mode. |
| 670 | static void test_serializing_empty_picture() { |
| robertphillips@google.com | 84b18c7 | 2014-04-13 19:09:42 +0000 | [diff] [blame] | 671 | SkPictureRecorder recorder; |
| commit-bot@chromium.org | 5fb2ce3 | 2014-04-17 23:35:06 +0000 | [diff] [blame^] | 672 | recorder.beginRecording(0, 0, NULL, 0); |
| robertphillips@google.com | 84b18c7 | 2014-04-13 19:09:42 +0000 | [diff] [blame] | 673 | SkAutoTUnref<SkPicture> picture(recorder.endRecording()); |
| scroggo@google.com | d614c6a | 2012-09-14 17:26:37 +0000 | [diff] [blame] | 674 | SkDynamicMemoryWStream stream; |
| robertphillips@google.com | 84b18c7 | 2014-04-13 19:09:42 +0000 | [diff] [blame] | 675 | picture->serialize(&stream); |
| scroggo@google.com | d614c6a | 2012-09-14 17:26:37 +0000 | [diff] [blame] | 676 | } |
| 677 | #endif |
| 678 | |
| commit-bot@chromium.org | e0e7cfe | 2013-09-09 20:09:12 +0000 | [diff] [blame] | 679 | static void rand_op(SkCanvas* canvas, SkRandom& rand) { |
| reed@google.com | 21b519d | 2012-10-02 17:42:15 +0000 | [diff] [blame] | 680 | SkPaint paint; |
| 681 | SkRect rect = SkRect::MakeWH(50, 50); |
| 682 | |
| 683 | SkScalar unit = rand.nextUScalar1(); |
| 684 | if (unit <= 0.3) { |
| 685 | // SkDebugf("save\n"); |
| 686 | canvas->save(); |
| 687 | } else if (unit <= 0.6) { |
| 688 | // SkDebugf("restore\n"); |
| 689 | canvas->restore(); |
| 690 | } else if (unit <= 0.9) { |
| 691 | // SkDebugf("clip\n"); |
| 692 | canvas->clipRect(rect); |
| 693 | } else { |
| 694 | // SkDebugf("draw\n"); |
| 695 | canvas->drawPaint(paint); |
| 696 | } |
| 697 | } |
| 698 | |
| commit-bot@chromium.org | ea7d08e | 2014-02-13 16:00:51 +0000 | [diff] [blame] | 699 | static void set_canvas_to_save_count_4(SkCanvas* canvas) { |
| 700 | canvas->restoreToCount(1); |
| 701 | canvas->save(); |
| 702 | canvas->save(); |
| 703 | canvas->save(); |
| 704 | } |
| 705 | |
| 706 | static void test_unbalanced_save_restores(skiatest::Reporter* reporter) { |
| 707 | SkCanvas testCanvas(100, 100); |
| 708 | set_canvas_to_save_count_4(&testCanvas); |
| 709 | |
| 710 | REPORTER_ASSERT(reporter, 4 == testCanvas.getSaveCount()); |
| 711 | |
| 712 | SkPaint paint; |
| 713 | SkRect rect = SkRect::MakeLTRB(-10000000, -10000000, 10000000, 10000000); |
| 714 | |
| robertphillips@google.com | 84b18c7 | 2014-04-13 19:09:42 +0000 | [diff] [blame] | 715 | SkPictureRecorder recorder; |
| commit-bot@chromium.org | ea7d08e | 2014-02-13 16:00:51 +0000 | [diff] [blame] | 716 | |
| robertphillips@google.com | 84b18c7 | 2014-04-13 19:09:42 +0000 | [diff] [blame] | 717 | { |
| 718 | // Create picture with 2 unbalanced saves |
| commit-bot@chromium.org | 5fb2ce3 | 2014-04-17 23:35:06 +0000 | [diff] [blame^] | 719 | SkCanvas* canvas = recorder.beginRecording(100, 100, NULL, 0); |
| robertphillips@google.com | 84b18c7 | 2014-04-13 19:09:42 +0000 | [diff] [blame] | 720 | canvas->save(); |
| 721 | canvas->translate(10, 10); |
| 722 | canvas->drawRect(rect, paint); |
| 723 | canvas->save(); |
| 724 | canvas->translate(10, 10); |
| 725 | canvas->drawRect(rect, paint); |
| 726 | SkAutoTUnref<SkPicture> extraSavePicture(recorder.endRecording()); |
| 727 | |
| 728 | testCanvas.drawPicture(*extraSavePicture); |
| 729 | REPORTER_ASSERT(reporter, 4 == testCanvas.getSaveCount()); |
| 730 | } |
| commit-bot@chromium.org | ea7d08e | 2014-02-13 16:00:51 +0000 | [diff] [blame] | 731 | |
| 732 | set_canvas_to_save_count_4(&testCanvas); |
| 733 | |
| robertphillips@google.com | 84b18c7 | 2014-04-13 19:09:42 +0000 | [diff] [blame] | 734 | { |
| 735 | // Create picture with 2 unbalanced restores |
| commit-bot@chromium.org | 5fb2ce3 | 2014-04-17 23:35:06 +0000 | [diff] [blame^] | 736 | SkCanvas* canvas = recorder.beginRecording(100, 100, NULL, 0); |
| robertphillips@google.com | 84b18c7 | 2014-04-13 19:09:42 +0000 | [diff] [blame] | 737 | canvas->save(); |
| 738 | canvas->translate(10, 10); |
| 739 | canvas->drawRect(rect, paint); |
| 740 | canvas->save(); |
| 741 | canvas->translate(10, 10); |
| 742 | canvas->drawRect(rect, paint); |
| 743 | canvas->restore(); |
| 744 | canvas->restore(); |
| 745 | canvas->restore(); |
| 746 | canvas->restore(); |
| 747 | SkAutoTUnref<SkPicture> extraRestorePicture(recorder.endRecording()); |
| commit-bot@chromium.org | ea7d08e | 2014-02-13 16:00:51 +0000 | [diff] [blame] | 748 | |
| robertphillips@google.com | 84b18c7 | 2014-04-13 19:09:42 +0000 | [diff] [blame] | 749 | testCanvas.drawPicture(*extraRestorePicture); |
| 750 | REPORTER_ASSERT(reporter, 4 == testCanvas.getSaveCount()); |
| 751 | } |
| commit-bot@chromium.org | ea7d08e | 2014-02-13 16:00:51 +0000 | [diff] [blame] | 752 | |
| robertphillips@google.com | 84b18c7 | 2014-04-13 19:09:42 +0000 | [diff] [blame] | 753 | set_canvas_to_save_count_4(&testCanvas); |
| commit-bot@chromium.org | ea7d08e | 2014-02-13 16:00:51 +0000 | [diff] [blame] | 754 | |
| robertphillips@google.com | 84b18c7 | 2014-04-13 19:09:42 +0000 | [diff] [blame] | 755 | { |
| commit-bot@chromium.org | 5fb2ce3 | 2014-04-17 23:35:06 +0000 | [diff] [blame^] | 756 | SkCanvas* canvas = recorder.beginRecording(100, 100, NULL, 0); |
| robertphillips@google.com | 84b18c7 | 2014-04-13 19:09:42 +0000 | [diff] [blame] | 757 | canvas->translate(10, 10); |
| 758 | canvas->drawRect(rect, paint); |
| 759 | SkAutoTUnref<SkPicture> noSavePicture(recorder.endRecording()); |
| 760 | |
| 761 | testCanvas.drawPicture(*noSavePicture); |
| 762 | REPORTER_ASSERT(reporter, 4 == testCanvas.getSaveCount()); |
| 763 | REPORTER_ASSERT(reporter, testCanvas.getTotalMatrix().isIdentity()); |
| 764 | } |
| commit-bot@chromium.org | ea7d08e | 2014-02-13 16:00:51 +0000 | [diff] [blame] | 765 | } |
| 766 | |
| sugoi@google.com | 54f0d1b | 2013-02-27 19:17:41 +0000 | [diff] [blame] | 767 | static void test_peephole() { |
| commit-bot@chromium.org | e0e7cfe | 2013-09-09 20:09:12 +0000 | [diff] [blame] | 768 | SkRandom rand; |
| reed@google.com | 21b519d | 2012-10-02 17:42:15 +0000 | [diff] [blame] | 769 | |
| robertphillips@google.com | 84b18c7 | 2014-04-13 19:09:42 +0000 | [diff] [blame] | 770 | SkPictureRecorder recorder; |
| 771 | |
| reed@google.com | 21b519d | 2012-10-02 17:42:15 +0000 | [diff] [blame] | 772 | for (int j = 0; j < 100; j++) { |
| commit-bot@chromium.org | e0e7cfe | 2013-09-09 20:09:12 +0000 | [diff] [blame] | 773 | SkRandom rand2(rand); // remember the seed |
| reed@google.com | 21b519d | 2012-10-02 17:42:15 +0000 | [diff] [blame] | 774 | |
| commit-bot@chromium.org | 5fb2ce3 | 2014-04-17 23:35:06 +0000 | [diff] [blame^] | 775 | SkCanvas* canvas = recorder.beginRecording(100, 100, NULL, 0); |
| reed@google.com | 21b519d | 2012-10-02 17:42:15 +0000 | [diff] [blame] | 776 | |
| 777 | for (int i = 0; i < 1000; ++i) { |
| 778 | rand_op(canvas, rand); |
| 779 | } |
| robertphillips@google.com | 84b18c7 | 2014-04-13 19:09:42 +0000 | [diff] [blame] | 780 | SkAutoTUnref<SkPicture> picture(recorder.endRecording()); |
| jvanverth@google.com | c490f80 | 2013-03-04 13:56:38 +0000 | [diff] [blame] | 781 | |
| 782 | rand = rand2; |
| reed@google.com | 21b519d | 2012-10-02 17:42:15 +0000 | [diff] [blame] | 783 | } |
| 784 | |
| 785 | { |
| commit-bot@chromium.org | 5fb2ce3 | 2014-04-17 23:35:06 +0000 | [diff] [blame^] | 786 | SkCanvas* canvas = recorder.beginRecording(100, 100, NULL, 0); |
| reed@google.com | 21b519d | 2012-10-02 17:42:15 +0000 | [diff] [blame] | 787 | SkRect rect = SkRect::MakeWH(50, 50); |
| skia.committer@gmail.com | 52c2437 | 2012-10-03 02:01:13 +0000 | [diff] [blame] | 788 | |
| reed@google.com | 21b519d | 2012-10-02 17:42:15 +0000 | [diff] [blame] | 789 | for (int i = 0; i < 100; ++i) { |
| 790 | canvas->save(); |
| 791 | } |
| 792 | while (canvas->getSaveCount() > 1) { |
| 793 | canvas->clipRect(rect); |
| 794 | canvas->restore(); |
| 795 | } |
| robertphillips@google.com | 84b18c7 | 2014-04-13 19:09:42 +0000 | [diff] [blame] | 796 | SkAutoTUnref<SkPicture> picture(recorder.endRecording()); |
| reed@google.com | 21b519d | 2012-10-02 17:42:15 +0000 | [diff] [blame] | 797 | } |
| 798 | } |
| 799 | |
| scroggo@google.com | 4b90b11 | 2012-12-04 15:08:56 +0000 | [diff] [blame] | 800 | #ifndef SK_DEBUG |
| 801 | // Only test this is in release mode. We deliberately crash in debug mode, since a valid caller |
| 802 | // should never do this. |
| 803 | static void test_bad_bitmap() { |
| 804 | // This bitmap has a width and height but no pixels. As a result, attempting to record it will |
| 805 | // fail. |
| 806 | SkBitmap bm; |
| mike@reedtribe.org | deee496 | 2014-02-13 14:41:43 +0000 | [diff] [blame] | 807 | bm.setConfig(SkImageInfo::MakeN32Premul(100, 100)); |
| robertphillips@google.com | 84b18c7 | 2014-04-13 19:09:42 +0000 | [diff] [blame] | 808 | SkPictureRecorder recorder; |
| commit-bot@chromium.org | 5fb2ce3 | 2014-04-17 23:35:06 +0000 | [diff] [blame^] | 809 | SkCanvas* recordingCanvas = recorder.beginRecording(100, 100, NULL, 0); |
| scroggo@google.com | 4b90b11 | 2012-12-04 15:08:56 +0000 | [diff] [blame] | 810 | recordingCanvas->drawBitmap(bm, 0, 0); |
| robertphillips@google.com | 84b18c7 | 2014-04-13 19:09:42 +0000 | [diff] [blame] | 811 | SkAutoTUnref<SkPicture> picture(recorder.endRecording()); |
| scroggo@google.com | 4b90b11 | 2012-12-04 15:08:56 +0000 | [diff] [blame] | 812 | |
| 813 | SkCanvas canvas; |
| robertphillips@google.com | 84b18c7 | 2014-04-13 19:09:42 +0000 | [diff] [blame] | 814 | canvas.drawPicture(*picture); |
| scroggo@google.com | 4b90b11 | 2012-12-04 15:08:56 +0000 | [diff] [blame] | 815 | } |
| 816 | #endif |
| 817 | |
| reed@google.com | 672588b | 2014-01-08 15:42:01 +0000 | [diff] [blame] | 818 | static SkData* encode_bitmap_to_data(size_t*, const SkBitmap& bm) { |
| scroggo@google.com | 1b1bcc3 | 2013-05-21 20:31:23 +0000 | [diff] [blame] | 819 | return SkImageEncoder::EncodeData(bm, SkImageEncoder::kPNG_Type, 100); |
| scroggo@google.com | 7c9d539 | 2012-12-10 15:40:55 +0000 | [diff] [blame] | 820 | } |
| 821 | |
| 822 | static SkData* serialized_picture_from_bitmap(const SkBitmap& bitmap) { |
| robertphillips@google.com | 84b18c7 | 2014-04-13 19:09:42 +0000 | [diff] [blame] | 823 | SkPictureRecorder recorder; |
| commit-bot@chromium.org | 5fb2ce3 | 2014-04-17 23:35:06 +0000 | [diff] [blame^] | 824 | SkCanvas* canvas = recorder.beginRecording(bitmap.width(), bitmap.height(), NULL, 0); |
| scroggo@google.com | 7c9d539 | 2012-12-10 15:40:55 +0000 | [diff] [blame] | 825 | canvas->drawBitmap(bitmap, 0, 0); |
| robertphillips@google.com | 84b18c7 | 2014-04-13 19:09:42 +0000 | [diff] [blame] | 826 | SkAutoTUnref<SkPicture> picture(recorder.endRecording()); |
| 827 | |
| scroggo@google.com | 7c9d539 | 2012-12-10 15:40:55 +0000 | [diff] [blame] | 828 | SkDynamicMemoryWStream wStream; |
| robertphillips@google.com | 84b18c7 | 2014-04-13 19:09:42 +0000 | [diff] [blame] | 829 | picture->serialize(&wStream, &encode_bitmap_to_data); |
| scroggo@google.com | 7c9d539 | 2012-12-10 15:40:55 +0000 | [diff] [blame] | 830 | return wStream.copyToData(); |
| 831 | } |
| 832 | |
| scroggo@google.com | 49ce11b | 2013-04-25 18:29:32 +0000 | [diff] [blame] | 833 | struct ErrorContext { |
| 834 | int fErrors; |
| 835 | skiatest::Reporter* fReporter; |
| 836 | }; |
| 837 | |
| 838 | static void assert_one_parse_error_cb(SkError error, void* context) { |
| 839 | ErrorContext* errorContext = static_cast<ErrorContext*>(context); |
| 840 | errorContext->fErrors++; |
| 841 | // This test only expects one error, and that is a kParseError. If there are others, |
| 842 | // there is some unknown problem. |
| 843 | REPORTER_ASSERT_MESSAGE(errorContext->fReporter, 1 == errorContext->fErrors, |
| 844 | "This threw more errors than expected."); |
| 845 | REPORTER_ASSERT_MESSAGE(errorContext->fReporter, kParseError_SkError == error, |
| 846 | SkGetLastErrorString()); |
| 847 | } |
| 848 | |
| scroggo@google.com | 7c9d539 | 2012-12-10 15:40:55 +0000 | [diff] [blame] | 849 | static void test_bitmap_with_encoded_data(skiatest::Reporter* reporter) { |
| 850 | // Create a bitmap that will be encoded. |
| 851 | SkBitmap original; |
| 852 | make_bm(&original, 100, 100, SK_ColorBLUE, true); |
| 853 | SkDynamicMemoryWStream wStream; |
| 854 | if (!SkImageEncoder::EncodeStream(&wStream, original, SkImageEncoder::kPNG_Type, 100)) { |
| 855 | return; |
| 856 | } |
| 857 | SkAutoDataUnref data(wStream.copyToData()); |
| scroggo@google.com | 7c9d539 | 2012-12-10 15:40:55 +0000 | [diff] [blame] | 858 | |
| scroggo@google.com | 7c9d539 | 2012-12-10 15:40:55 +0000 | [diff] [blame] | 859 | SkBitmap bm; |
| halcanary@google.com | 3d50ea1 | 2014-01-02 13:15:13 +0000 | [diff] [blame] | 860 | bool installSuccess = SkInstallDiscardablePixelRef( |
| 861 | SkDecodingImageGenerator::Create(data, SkDecodingImageGenerator::Options()), &bm, NULL); |
| reed@google.com | bf79023 | 2013-12-13 19:45:58 +0000 | [diff] [blame] | 862 | REPORTER_ASSERT(reporter, installSuccess); |
| scroggo@google.com | 7c9d539 | 2012-12-10 15:40:55 +0000 | [diff] [blame] | 863 | |
| 864 | // Write both bitmaps to pictures, and ensure that the resulting data streams are the same. |
| 865 | // Flattening original will follow the old path of performing an encode, while flattening bm |
| 866 | // will use the already encoded data. |
| 867 | SkAutoDataUnref picture1(serialized_picture_from_bitmap(original)); |
| 868 | SkAutoDataUnref picture2(serialized_picture_from_bitmap(bm)); |
| 869 | REPORTER_ASSERT(reporter, picture1->equals(picture2)); |
| scroggo@google.com | 49ce11b | 2013-04-25 18:29:32 +0000 | [diff] [blame] | 870 | // Now test that a parse error was generated when trying to create a new SkPicture without |
| 871 | // providing a function to decode the bitmap. |
| 872 | ErrorContext context; |
| 873 | context.fErrors = 0; |
| 874 | context.fReporter = reporter; |
| 875 | SkSetErrorCallback(assert_one_parse_error_cb, &context); |
| 876 | SkMemoryStream pictureStream(picture1); |
| scroggo@google.com | 49ce11b | 2013-04-25 18:29:32 +0000 | [diff] [blame] | 877 | SkClearLastError(); |
| scroggo@google.com | f1754ec | 2013-06-28 21:32:00 +0000 | [diff] [blame] | 878 | SkAutoUnref pictureFromStream(SkPicture::CreateFromStream(&pictureStream, NULL)); |
| 879 | REPORTER_ASSERT(reporter, pictureFromStream.get() != NULL); |
| scroggo@google.com | 49ce11b | 2013-04-25 18:29:32 +0000 | [diff] [blame] | 880 | SkClearLastError(); |
| 881 | SkSetErrorCallback(NULL, NULL); |
| scroggo@google.com | 7c9d539 | 2012-12-10 15:40:55 +0000 | [diff] [blame] | 882 | } |
| 883 | |
| junov@chromium.org | 94f20dc | 2013-01-28 21:04:44 +0000 | [diff] [blame] | 884 | static void test_clone_empty(skiatest::Reporter* reporter) { |
| 885 | // This is a regression test for crbug.com/172062 |
| 886 | // Before the fix, we used to crash accessing a null pointer when we |
| 887 | // had a picture with no paints. This test passes by not crashing. |
| 888 | { |
| robertphillips@google.com | 84b18c7 | 2014-04-13 19:09:42 +0000 | [diff] [blame] | 889 | SkPictureRecorder recorder; |
| commit-bot@chromium.org | 5fb2ce3 | 2014-04-17 23:35:06 +0000 | [diff] [blame^] | 890 | recorder.beginRecording(1, 1, NULL, 0); |
| robertphillips@google.com | 84b18c7 | 2014-04-13 19:09:42 +0000 | [diff] [blame] | 891 | SkAutoTUnref<SkPicture> picture(recorder.endRecording()); |
| 892 | SkAutoTUnref<SkPicture> destPicture(picture->clone()); |
| junov@chromium.org | 94f20dc | 2013-01-28 21:04:44 +0000 | [diff] [blame] | 893 | REPORTER_ASSERT(reporter, NULL != destPicture); |
| junov@chromium.org | 94f20dc | 2013-01-28 21:04:44 +0000 | [diff] [blame] | 894 | } |
| 895 | } |
| 896 | |
| commit-bot@chromium.org | 70512af | 2014-03-18 17:45:32 +0000 | [diff] [blame] | 897 | static void test_draw_empty(skiatest::Reporter* reporter) { |
| 898 | SkBitmap result; |
| 899 | make_bm(&result, 2, 2, SK_ColorBLACK, false); |
| 900 | |
| 901 | SkCanvas canvas(result); |
| 902 | |
| 903 | { |
| 904 | // stock SkPicture |
| robertphillips@google.com | 84b18c7 | 2014-04-13 19:09:42 +0000 | [diff] [blame] | 905 | SkPictureRecorder recorder; |
| commit-bot@chromium.org | 5fb2ce3 | 2014-04-17 23:35:06 +0000 | [diff] [blame^] | 906 | recorder.beginRecording(1, 1, NULL, 0); |
| robertphillips@google.com | 84b18c7 | 2014-04-13 19:09:42 +0000 | [diff] [blame] | 907 | SkAutoTUnref<SkPicture> picture(recorder.endRecording()); |
| commit-bot@chromium.org | 70512af | 2014-03-18 17:45:32 +0000 | [diff] [blame] | 908 | |
| robertphillips@google.com | 84b18c7 | 2014-04-13 19:09:42 +0000 | [diff] [blame] | 909 | canvas.drawPicture(*picture); |
| commit-bot@chromium.org | 70512af | 2014-03-18 17:45:32 +0000 | [diff] [blame] | 910 | } |
| 911 | |
| 912 | { |
| 913 | // tile grid |
| commit-bot@chromium.org | 5fb2ce3 | 2014-04-17 23:35:06 +0000 | [diff] [blame^] | 914 | SkTileGridFactory::TileGridInfo gridInfo; |
| commit-bot@chromium.org | 70512af | 2014-03-18 17:45:32 +0000 | [diff] [blame] | 915 | gridInfo.fMargin.setEmpty(); |
| 916 | gridInfo.fOffset.setZero(); |
| 917 | gridInfo.fTileInterval.set(1, 1); |
| 918 | |
| commit-bot@chromium.org | 5fb2ce3 | 2014-04-17 23:35:06 +0000 | [diff] [blame^] | 919 | SkTileGridFactory factory(gridInfo); |
| 920 | SkPictureRecorder recorder; |
| 921 | recorder.beginRecording(1, 1, &factory, 0); |
| robertphillips@google.com | 84b18c7 | 2014-04-13 19:09:42 +0000 | [diff] [blame] | 922 | SkAutoTUnref<SkPicture> picture(recorder.endRecording()); |
| commit-bot@chromium.org | 70512af | 2014-03-18 17:45:32 +0000 | [diff] [blame] | 923 | |
| robertphillips@google.com | 84b18c7 | 2014-04-13 19:09:42 +0000 | [diff] [blame] | 924 | canvas.drawPicture(*picture); |
| commit-bot@chromium.org | 70512af | 2014-03-18 17:45:32 +0000 | [diff] [blame] | 925 | } |
| 926 | |
| 927 | { |
| 928 | // RTree |
| commit-bot@chromium.org | 5fb2ce3 | 2014-04-17 23:35:06 +0000 | [diff] [blame^] | 929 | SkRTreeFactory factory; |
| 930 | SkPictureRecorder recorder; |
| 931 | recorder.beginRecording(1, 1, &factory, 0); |
| robertphillips@google.com | 84b18c7 | 2014-04-13 19:09:42 +0000 | [diff] [blame] | 932 | SkAutoTUnref<SkPicture> picture(recorder.endRecording()); |
| commit-bot@chromium.org | 70512af | 2014-03-18 17:45:32 +0000 | [diff] [blame] | 933 | |
| robertphillips@google.com | 84b18c7 | 2014-04-13 19:09:42 +0000 | [diff] [blame] | 934 | canvas.drawPicture(*picture); |
| commit-bot@chromium.org | 70512af | 2014-03-18 17:45:32 +0000 | [diff] [blame] | 935 | } |
| 936 | |
| 937 | { |
| 938 | // quad tree |
| commit-bot@chromium.org | 5fb2ce3 | 2014-04-17 23:35:06 +0000 | [diff] [blame^] | 939 | SkQuadTreeFactory factory; |
| 940 | SkPictureRecorder recorder; |
| 941 | recorder.beginRecording(1, 1, &factory, 0); |
| robertphillips@google.com | 84b18c7 | 2014-04-13 19:09:42 +0000 | [diff] [blame] | 942 | SkAutoTUnref<SkPicture> picture(recorder.endRecording()); |
| commit-bot@chromium.org | 70512af | 2014-03-18 17:45:32 +0000 | [diff] [blame] | 943 | |
| robertphillips@google.com | 84b18c7 | 2014-04-13 19:09:42 +0000 | [diff] [blame] | 944 | canvas.drawPicture(*picture); |
| commit-bot@chromium.org | 70512af | 2014-03-18 17:45:32 +0000 | [diff] [blame] | 945 | } |
| 946 | } |
| 947 | |
| junov@chromium.org | d575eed | 2013-05-08 15:39:13 +0000 | [diff] [blame] | 948 | static void test_clip_bound_opt(skiatest::Reporter* reporter) { |
| 949 | // Test for crbug.com/229011 |
| 950 | SkRect rect1 = SkRect::MakeXYWH(SkIntToScalar(4), SkIntToScalar(4), |
| 951 | SkIntToScalar(2), SkIntToScalar(2)); |
| 952 | SkRect rect2 = SkRect::MakeXYWH(SkIntToScalar(7), SkIntToScalar(7), |
| 953 | SkIntToScalar(1), SkIntToScalar(1)); |
| 954 | SkRect rect3 = SkRect::MakeXYWH(SkIntToScalar(6), SkIntToScalar(6), |
| 955 | SkIntToScalar(1), SkIntToScalar(1)); |
| 956 | |
| 957 | SkPath invPath; |
| 958 | invPath.addOval(rect1); |
| 959 | invPath.setFillType(SkPath::kInverseEvenOdd_FillType); |
| 960 | SkPath path; |
| 961 | path.addOval(rect2); |
| 962 | SkPath path2; |
| 963 | path2.addOval(rect3); |
| 964 | SkIRect clipBounds; |
| robertphillips@google.com | 84b18c7 | 2014-04-13 19:09:42 +0000 | [diff] [blame] | 965 | SkPictureRecorder recorder; |
| junov@chromium.org | d575eed | 2013-05-08 15:39:13 +0000 | [diff] [blame] | 966 | // Minimalist test set for 100% code coverage of |
| 967 | // SkPictureRecord::updateClipConservativelyUsingBounds |
| 968 | { |
| commit-bot@chromium.org | 5fb2ce3 | 2014-04-17 23:35:06 +0000 | [diff] [blame^] | 969 | SkCanvas* canvas = recorder.beginRecording(10, 10, NULL, |
| junov@chromium.org | d575eed | 2013-05-08 15:39:13 +0000 | [diff] [blame] | 970 | SkPicture::kUsePathBoundsForClip_RecordingFlag); |
| 971 | canvas->clipPath(invPath, SkRegion::kIntersect_Op); |
| 972 | bool nonEmpty = canvas->getClipDeviceBounds(&clipBounds); |
| 973 | REPORTER_ASSERT(reporter, true == nonEmpty); |
| 974 | REPORTER_ASSERT(reporter, 0 == clipBounds.fLeft); |
| 975 | REPORTER_ASSERT(reporter, 0 == clipBounds.fTop); |
| 976 | REPORTER_ASSERT(reporter, 10 == clipBounds.fBottom); |
| 977 | REPORTER_ASSERT(reporter, 10 == clipBounds.fRight); |
| 978 | } |
| 979 | { |
| commit-bot@chromium.org | 5fb2ce3 | 2014-04-17 23:35:06 +0000 | [diff] [blame^] | 980 | SkCanvas* canvas = recorder.beginRecording(10, 10, NULL, |
| junov@chromium.org | d575eed | 2013-05-08 15:39:13 +0000 | [diff] [blame] | 981 | SkPicture::kUsePathBoundsForClip_RecordingFlag); |
| 982 | canvas->clipPath(path, SkRegion::kIntersect_Op); |
| 983 | canvas->clipPath(invPath, SkRegion::kIntersect_Op); |
| 984 | bool nonEmpty = canvas->getClipDeviceBounds(&clipBounds); |
| 985 | REPORTER_ASSERT(reporter, true == nonEmpty); |
| 986 | REPORTER_ASSERT(reporter, 7 == clipBounds.fLeft); |
| 987 | REPORTER_ASSERT(reporter, 7 == clipBounds.fTop); |
| 988 | REPORTER_ASSERT(reporter, 8 == clipBounds.fBottom); |
| 989 | REPORTER_ASSERT(reporter, 8 == clipBounds.fRight); |
| 990 | } |
| 991 | { |
| commit-bot@chromium.org | 5fb2ce3 | 2014-04-17 23:35:06 +0000 | [diff] [blame^] | 992 | SkCanvas* canvas = recorder.beginRecording(10, 10, NULL, |
| junov@chromium.org | d575eed | 2013-05-08 15:39:13 +0000 | [diff] [blame] | 993 | SkPicture::kUsePathBoundsForClip_RecordingFlag); |
| 994 | canvas->clipPath(path, SkRegion::kIntersect_Op); |
| 995 | canvas->clipPath(invPath, SkRegion::kUnion_Op); |
| 996 | bool nonEmpty = canvas->getClipDeviceBounds(&clipBounds); |
| 997 | REPORTER_ASSERT(reporter, true == nonEmpty); |
| 998 | REPORTER_ASSERT(reporter, 0 == clipBounds.fLeft); |
| 999 | REPORTER_ASSERT(reporter, 0 == clipBounds.fTop); |
| 1000 | REPORTER_ASSERT(reporter, 10 == clipBounds.fBottom); |
| 1001 | REPORTER_ASSERT(reporter, 10 == clipBounds.fRight); |
| 1002 | } |
| 1003 | { |
| commit-bot@chromium.org | 5fb2ce3 | 2014-04-17 23:35:06 +0000 | [diff] [blame^] | 1004 | SkCanvas* canvas = recorder.beginRecording(10, 10, NULL, |
| junov@chromium.org | d575eed | 2013-05-08 15:39:13 +0000 | [diff] [blame] | 1005 | SkPicture::kUsePathBoundsForClip_RecordingFlag); |
| 1006 | canvas->clipPath(path, SkRegion::kDifference_Op); |
| 1007 | bool nonEmpty = canvas->getClipDeviceBounds(&clipBounds); |
| 1008 | REPORTER_ASSERT(reporter, true == nonEmpty); |
| 1009 | REPORTER_ASSERT(reporter, 0 == clipBounds.fLeft); |
| 1010 | REPORTER_ASSERT(reporter, 0 == clipBounds.fTop); |
| 1011 | REPORTER_ASSERT(reporter, 10 == clipBounds.fBottom); |
| 1012 | REPORTER_ASSERT(reporter, 10 == clipBounds.fRight); |
| 1013 | } |
| 1014 | { |
| commit-bot@chromium.org | 5fb2ce3 | 2014-04-17 23:35:06 +0000 | [diff] [blame^] | 1015 | SkCanvas* canvas = recorder.beginRecording(10, 10, NULL, |
| junov@chromium.org | d575eed | 2013-05-08 15:39:13 +0000 | [diff] [blame] | 1016 | SkPicture::kUsePathBoundsForClip_RecordingFlag); |
| 1017 | canvas->clipPath(path, SkRegion::kReverseDifference_Op); |
| 1018 | bool nonEmpty = canvas->getClipDeviceBounds(&clipBounds); |
| 1019 | // True clip is actually empty in this case, but the best |
| 1020 | // determination we can make using only bounds as input is that the |
| 1021 | // clip is included in the bounds of 'path'. |
| 1022 | REPORTER_ASSERT(reporter, true == nonEmpty); |
| 1023 | REPORTER_ASSERT(reporter, 7 == clipBounds.fLeft); |
| 1024 | REPORTER_ASSERT(reporter, 7 == clipBounds.fTop); |
| 1025 | REPORTER_ASSERT(reporter, 8 == clipBounds.fBottom); |
| 1026 | REPORTER_ASSERT(reporter, 8 == clipBounds.fRight); |
| 1027 | } |
| 1028 | { |
| commit-bot@chromium.org | 5fb2ce3 | 2014-04-17 23:35:06 +0000 | [diff] [blame^] | 1029 | SkCanvas* canvas = recorder.beginRecording(10, 10, NULL, |
| junov@chromium.org | d575eed | 2013-05-08 15:39:13 +0000 | [diff] [blame] | 1030 | SkPicture::kUsePathBoundsForClip_RecordingFlag); |
| 1031 | canvas->clipPath(path, SkRegion::kIntersect_Op); |
| 1032 | canvas->clipPath(path2, SkRegion::kXOR_Op); |
| 1033 | bool nonEmpty = canvas->getClipDeviceBounds(&clipBounds); |
| 1034 | REPORTER_ASSERT(reporter, true == nonEmpty); |
| 1035 | REPORTER_ASSERT(reporter, 6 == clipBounds.fLeft); |
| 1036 | REPORTER_ASSERT(reporter, 6 == clipBounds.fTop); |
| 1037 | REPORTER_ASSERT(reporter, 8 == clipBounds.fBottom); |
| 1038 | REPORTER_ASSERT(reporter, 8 == clipBounds.fRight); |
| 1039 | } |
| 1040 | } |
| 1041 | |
| fmalita@google.com | d0f1a4f | 2013-08-27 15:50:19 +0000 | [diff] [blame] | 1042 | /** |
| 1043 | * A canvas that records the number of clip commands. |
| 1044 | */ |
| 1045 | class ClipCountingCanvas : public SkCanvas { |
| 1046 | public: |
| commit-bot@chromium.org | e254310 | 2014-01-31 19:42:58 +0000 | [diff] [blame] | 1047 | explicit ClipCountingCanvas(int width, int height) |
| 1048 | : INHERITED(width, height) |
| fmalita@google.com | d0f1a4f | 2013-08-27 15:50:19 +0000 | [diff] [blame] | 1049 | , fClipCount(0){ |
| 1050 | } |
| 1051 | |
| skia.committer@gmail.com | 370a899 | 2014-03-01 03:02:09 +0000 | [diff] [blame] | 1052 | virtual void onClipRect(const SkRect& r, |
| 1053 | SkRegion::Op op, |
| robertphillips@google.com | 8f90a89 | 2014-02-28 18:19:39 +0000 | [diff] [blame] | 1054 | ClipEdgeStyle edgeStyle) SK_OVERRIDE { |
| fmalita@google.com | d0f1a4f | 2013-08-27 15:50:19 +0000 | [diff] [blame] | 1055 | fClipCount += 1; |
| robertphillips@google.com | 8f90a89 | 2014-02-28 18:19:39 +0000 | [diff] [blame] | 1056 | this->INHERITED::onClipRect(r, op, edgeStyle); |
| fmalita@google.com | d0f1a4f | 2013-08-27 15:50:19 +0000 | [diff] [blame] | 1057 | } |
| 1058 | |
| skia.committer@gmail.com | 370a899 | 2014-03-01 03:02:09 +0000 | [diff] [blame] | 1059 | virtual void onClipRRect(const SkRRect& rrect, |
| 1060 | SkRegion::Op op, |
| robertphillips@google.com | 8f90a89 | 2014-02-28 18:19:39 +0000 | [diff] [blame] | 1061 | ClipEdgeStyle edgeStyle)SK_OVERRIDE { |
| fmalita@google.com | d0f1a4f | 2013-08-27 15:50:19 +0000 | [diff] [blame] | 1062 | fClipCount += 1; |
| robertphillips@google.com | 8f90a89 | 2014-02-28 18:19:39 +0000 | [diff] [blame] | 1063 | this->INHERITED::onClipRRect(rrect, op, edgeStyle); |
| fmalita@google.com | d0f1a4f | 2013-08-27 15:50:19 +0000 | [diff] [blame] | 1064 | } |
| 1065 | |
| skia.committer@gmail.com | 370a899 | 2014-03-01 03:02:09 +0000 | [diff] [blame] | 1066 | virtual void onClipPath(const SkPath& path, |
| 1067 | SkRegion::Op op, |
| robertphillips@google.com | 8f90a89 | 2014-02-28 18:19:39 +0000 | [diff] [blame] | 1068 | ClipEdgeStyle edgeStyle) SK_OVERRIDE { |
| fmalita@google.com | d0f1a4f | 2013-08-27 15:50:19 +0000 | [diff] [blame] | 1069 | fClipCount += 1; |
| robertphillips@google.com | 8f90a89 | 2014-02-28 18:19:39 +0000 | [diff] [blame] | 1070 | this->INHERITED::onClipPath(path, op, edgeStyle); |
| 1071 | } |
| 1072 | |
| 1073 | virtual void onClipRegion(const SkRegion& deviceRgn, SkRegion::Op op) SK_OVERRIDE { |
| 1074 | fClipCount += 1; |
| 1075 | this->INHERITED::onClipRegion(deviceRgn, op); |
| fmalita@google.com | d0f1a4f | 2013-08-27 15:50:19 +0000 | [diff] [blame] | 1076 | } |
| 1077 | |
| 1078 | unsigned getClipCount() const { return fClipCount; } |
| 1079 | |
| 1080 | private: |
| 1081 | unsigned fClipCount; |
| 1082 | |
| 1083 | typedef SkCanvas INHERITED; |
| 1084 | }; |
| 1085 | |
| 1086 | static void test_clip_expansion(skiatest::Reporter* reporter) { |
| robertphillips@google.com | 84b18c7 | 2014-04-13 19:09:42 +0000 | [diff] [blame] | 1087 | SkPictureRecorder recorder; |
| commit-bot@chromium.org | 5fb2ce3 | 2014-04-17 23:35:06 +0000 | [diff] [blame^] | 1088 | SkCanvas* canvas = recorder.beginRecording(10, 10, NULL, 0); |
| fmalita@google.com | d0f1a4f | 2013-08-27 15:50:19 +0000 | [diff] [blame] | 1089 | |
| 1090 | canvas->clipRect(SkRect::MakeEmpty(), SkRegion::kReplace_Op); |
| 1091 | // The following expanding clip should not be skipped. |
| 1092 | canvas->clipRect(SkRect::MakeXYWH(4, 4, 3, 3), SkRegion::kUnion_Op); |
| 1093 | // Draw something so the optimizer doesn't just fold the world. |
| 1094 | SkPaint p; |
| 1095 | p.setColor(SK_ColorBLUE); |
| 1096 | canvas->drawPaint(p); |
| robertphillips@google.com | 84b18c7 | 2014-04-13 19:09:42 +0000 | [diff] [blame] | 1097 | SkAutoTUnref<SkPicture> picture(recorder.endRecording()); |
| fmalita@google.com | d0f1a4f | 2013-08-27 15:50:19 +0000 | [diff] [blame] | 1098 | |
| commit-bot@chromium.org | e254310 | 2014-01-31 19:42:58 +0000 | [diff] [blame] | 1099 | ClipCountingCanvas testCanvas(10, 10); |
| robertphillips@google.com | 84b18c7 | 2014-04-13 19:09:42 +0000 | [diff] [blame] | 1100 | picture->draw(&testCanvas); |
| fmalita@google.com | d0f1a4f | 2013-08-27 15:50:19 +0000 | [diff] [blame] | 1101 | |
| 1102 | // Both clips should be present on playback. |
| 1103 | REPORTER_ASSERT(reporter, testCanvas.getClipCount() == 2); |
| 1104 | } |
| 1105 | |
| tomhudson@google.com | 381010e | 2013-10-24 11:12:47 +0000 | [diff] [blame] | 1106 | static void test_hierarchical(skiatest::Reporter* reporter) { |
| 1107 | SkBitmap bm; |
| 1108 | make_bm(&bm, 10, 10, SK_ColorRED, true); |
| 1109 | |
| robertphillips@google.com | 84b18c7 | 2014-04-13 19:09:42 +0000 | [diff] [blame] | 1110 | SkPictureRecorder recorder; |
| tomhudson@google.com | 381010e | 2013-10-24 11:12:47 +0000 | [diff] [blame] | 1111 | |
| commit-bot@chromium.org | 5fb2ce3 | 2014-04-17 23:35:06 +0000 | [diff] [blame^] | 1112 | recorder.beginRecording(10, 10, NULL, 0); |
| robertphillips@google.com | 84b18c7 | 2014-04-13 19:09:42 +0000 | [diff] [blame] | 1113 | SkAutoTUnref<SkPicture> childPlain(recorder.endRecording()); |
| 1114 | REPORTER_ASSERT(reporter, !childPlain->willPlayBackBitmaps()); // 0 |
| tomhudson@google.com | 381010e | 2013-10-24 11:12:47 +0000 | [diff] [blame] | 1115 | |
| commit-bot@chromium.org | 5fb2ce3 | 2014-04-17 23:35:06 +0000 | [diff] [blame^] | 1116 | recorder.beginRecording(10, 10, NULL, 0)->drawBitmap(bm, 0, 0); |
| robertphillips@google.com | 84b18c7 | 2014-04-13 19:09:42 +0000 | [diff] [blame] | 1117 | SkAutoTUnref<SkPicture> childWithBitmap(recorder.endRecording()); |
| 1118 | REPORTER_ASSERT(reporter, childWithBitmap->willPlayBackBitmaps()); // 1 |
| tomhudson@google.com | 381010e | 2013-10-24 11:12:47 +0000 | [diff] [blame] | 1119 | |
| robertphillips@google.com | 84b18c7 | 2014-04-13 19:09:42 +0000 | [diff] [blame] | 1120 | { |
| commit-bot@chromium.org | 5fb2ce3 | 2014-04-17 23:35:06 +0000 | [diff] [blame^] | 1121 | SkCanvas* canvas = recorder.beginRecording(10, 10, NULL, 0); |
| robertphillips@google.com | 84b18c7 | 2014-04-13 19:09:42 +0000 | [diff] [blame] | 1122 | canvas->drawPicture(*childPlain); |
| 1123 | SkAutoTUnref<SkPicture> parentPP(recorder.endRecording()); |
| 1124 | REPORTER_ASSERT(reporter, !parentPP->willPlayBackBitmaps()); // 0 |
| 1125 | } |
| 1126 | { |
| commit-bot@chromium.org | 5fb2ce3 | 2014-04-17 23:35:06 +0000 | [diff] [blame^] | 1127 | SkCanvas* canvas = recorder.beginRecording(10, 10, NULL, 0); |
| robertphillips@google.com | 84b18c7 | 2014-04-13 19:09:42 +0000 | [diff] [blame] | 1128 | canvas->drawPicture(*childWithBitmap); |
| 1129 | SkAutoTUnref<SkPicture> parentPWB(recorder.endRecording()); |
| 1130 | REPORTER_ASSERT(reporter, parentPWB->willPlayBackBitmaps()); // 1 |
| 1131 | } |
| 1132 | { |
| commit-bot@chromium.org | 5fb2ce3 | 2014-04-17 23:35:06 +0000 | [diff] [blame^] | 1133 | SkCanvas* canvas = recorder.beginRecording(10, 10, NULL, 0); |
| robertphillips@google.com | 84b18c7 | 2014-04-13 19:09:42 +0000 | [diff] [blame] | 1134 | canvas->drawBitmap(bm, 0, 0); |
| 1135 | canvas->drawPicture(*childPlain); |
| 1136 | SkAutoTUnref<SkPicture> parentWBP(recorder.endRecording()); |
| 1137 | REPORTER_ASSERT(reporter, parentWBP->willPlayBackBitmaps()); // 1 |
| 1138 | } |
| 1139 | { |
| commit-bot@chromium.org | 5fb2ce3 | 2014-04-17 23:35:06 +0000 | [diff] [blame^] | 1140 | SkCanvas* canvas = recorder.beginRecording(10, 10, NULL, 0); |
| robertphillips@google.com | 84b18c7 | 2014-04-13 19:09:42 +0000 | [diff] [blame] | 1141 | canvas->drawBitmap(bm, 0, 0); |
| 1142 | canvas->drawPicture(*childWithBitmap); |
| 1143 | SkAutoTUnref<SkPicture> parentWBWB(recorder.endRecording()); |
| 1144 | REPORTER_ASSERT(reporter, parentWBWB->willPlayBackBitmaps()); // 2 |
| 1145 | } |
| tomhudson@google.com | 381010e | 2013-10-24 11:12:47 +0000 | [diff] [blame] | 1146 | } |
| 1147 | |
| robertphillips@google.com | d550088 | 2014-04-02 23:51:13 +0000 | [diff] [blame] | 1148 | static void test_gen_id(skiatest::Reporter* reporter) { |
| 1149 | |
| robertphillips@google.com | 84b18c7 | 2014-04-13 19:09:42 +0000 | [diff] [blame] | 1150 | SkPicture empty; |
| robertphillips@google.com | d550088 | 2014-04-02 23:51:13 +0000 | [diff] [blame] | 1151 | |
| robertphillips@google.com | 84b18c7 | 2014-04-13 19:09:42 +0000 | [diff] [blame] | 1152 | // Empty pictures should still have a valid ID |
| 1153 | REPORTER_ASSERT(reporter, empty.uniqueID() != SK_InvalidGenID); |
| robertphillips@google.com | d550088 | 2014-04-02 23:51:13 +0000 | [diff] [blame] | 1154 | |
| robertphillips@google.com | 84b18c7 | 2014-04-13 19:09:42 +0000 | [diff] [blame] | 1155 | SkPictureRecorder recorder; |
| robertphillips@google.com | d550088 | 2014-04-02 23:51:13 +0000 | [diff] [blame] | 1156 | |
| commit-bot@chromium.org | 5fb2ce3 | 2014-04-17 23:35:06 +0000 | [diff] [blame^] | 1157 | SkCanvas* canvas = recorder.beginRecording(1, 1, NULL, 0); |
| robertphillips@google.com | 84b18c7 | 2014-04-13 19:09:42 +0000 | [diff] [blame] | 1158 | canvas->drawARGB(255, 255, 255, 255); |
| 1159 | SkAutoTUnref<SkPicture> hasData(recorder.endRecording()); |
| 1160 | // picture should have a non-zero id after recording |
| 1161 | REPORTER_ASSERT(reporter, hasData->uniqueID() != SK_InvalidGenID); |
| robertphillips@google.com | d550088 | 2014-04-02 23:51:13 +0000 | [diff] [blame] | 1162 | |
| robertphillips@google.com | 84b18c7 | 2014-04-13 19:09:42 +0000 | [diff] [blame] | 1163 | // both pictures should have different ids |
| 1164 | REPORTER_ASSERT(reporter, hasData->uniqueID() != empty.uniqueID()); |
| robertphillips@google.com | d550088 | 2014-04-02 23:51:13 +0000 | [diff] [blame] | 1165 | |
| 1166 | // test out copy constructor |
| robertphillips@google.com | 84b18c7 | 2014-04-13 19:09:42 +0000 | [diff] [blame] | 1167 | SkPicture copyWithData(*hasData); |
| 1168 | REPORTER_ASSERT(reporter, hasData->uniqueID() == copyWithData.uniqueID()); |
| robertphillips@google.com | d550088 | 2014-04-02 23:51:13 +0000 | [diff] [blame] | 1169 | |
| 1170 | SkPicture emptyCopy(empty); |
| commit-bot@chromium.org | 2b4e370 | 2014-04-07 18:26:22 +0000 | [diff] [blame] | 1171 | REPORTER_ASSERT(reporter, empty.uniqueID() != emptyCopy.uniqueID()); |
| skia.committer@gmail.com | a915772 | 2014-04-03 03:04:26 +0000 | [diff] [blame] | 1172 | |
| robertphillips@google.com | d550088 | 2014-04-02 23:51:13 +0000 | [diff] [blame] | 1173 | // test out swap |
| robertphillips@google.com | 84b18c7 | 2014-04-13 19:09:42 +0000 | [diff] [blame] | 1174 | { |
| 1175 | SkPicture swapWithData; |
| 1176 | uint32_t beforeID1 = swapWithData.uniqueID(); |
| 1177 | uint32_t beforeID2 = copyWithData.uniqueID(); |
| 1178 | swapWithData.swap(copyWithData); |
| 1179 | REPORTER_ASSERT(reporter, copyWithData.uniqueID() == beforeID1); |
| 1180 | REPORTER_ASSERT(reporter, swapWithData.uniqueID() == beforeID2); |
| 1181 | } |
| robertphillips@google.com | d550088 | 2014-04-02 23:51:13 +0000 | [diff] [blame] | 1182 | |
| 1183 | // test out clone |
| robertphillips@google.com | 84b18c7 | 2014-04-13 19:09:42 +0000 | [diff] [blame] | 1184 | { |
| 1185 | SkAutoTUnref<SkPicture> cloneWithData(hasData->clone()); |
| 1186 | REPORTER_ASSERT(reporter, hasData->uniqueID() == cloneWithData->uniqueID()); |
| robertphillips@google.com | d550088 | 2014-04-02 23:51:13 +0000 | [diff] [blame] | 1187 | |
| robertphillips@google.com | 84b18c7 | 2014-04-13 19:09:42 +0000 | [diff] [blame] | 1188 | SkAutoTUnref<SkPicture> emptyClone(empty.clone()); |
| 1189 | REPORTER_ASSERT(reporter, empty.uniqueID() != emptyClone->uniqueID()); |
| 1190 | } |
| robertphillips@google.com | d550088 | 2014-04-02 23:51:13 +0000 | [diff] [blame] | 1191 | } |
| 1192 | |
| tfarina@chromium.org | e4fafb1 | 2013-12-12 21:11:12 +0000 | [diff] [blame] | 1193 | DEF_TEST(Picture, reporter) { |
| scroggo@google.com | d614c6a | 2012-09-14 17:26:37 +0000 | [diff] [blame] | 1194 | #ifdef SK_DEBUG |
| 1195 | test_deleting_empty_playback(); |
| 1196 | test_serializing_empty_picture(); |
| scroggo@google.com | 4b90b11 | 2012-12-04 15:08:56 +0000 | [diff] [blame] | 1197 | #else |
| 1198 | test_bad_bitmap(); |
| scroggo@google.com | d614c6a | 2012-09-14 17:26:37 +0000 | [diff] [blame] | 1199 | #endif |
| commit-bot@chromium.org | ea7d08e | 2014-02-13 16:00:51 +0000 | [diff] [blame] | 1200 | test_unbalanced_save_restores(reporter); |
| sugoi@google.com | 54f0d1b | 2013-02-27 19:17:41 +0000 | [diff] [blame] | 1201 | test_peephole(); |
| reed@google.com | fe7b1ed | 2012-11-29 21:00:39 +0000 | [diff] [blame] | 1202 | test_gatherpixelrefs(reporter); |
| robertphillips@google.com | 56bf6e4 | 2014-01-13 13:33:26 +0000 | [diff] [blame] | 1203 | test_gatherpixelrefsandrects(reporter); |
| scroggo@google.com | 7c9d539 | 2012-12-10 15:40:55 +0000 | [diff] [blame] | 1204 | test_bitmap_with_encoded_data(reporter); |
| junov@chromium.org | 94f20dc | 2013-01-28 21:04:44 +0000 | [diff] [blame] | 1205 | test_clone_empty(reporter); |
| commit-bot@chromium.org | 70512af | 2014-03-18 17:45:32 +0000 | [diff] [blame] | 1206 | test_draw_empty(reporter); |
| junov@chromium.org | d575eed | 2013-05-08 15:39:13 +0000 | [diff] [blame] | 1207 | test_clip_bound_opt(reporter); |
| fmalita@google.com | d0f1a4f | 2013-08-27 15:50:19 +0000 | [diff] [blame] | 1208 | test_clip_expansion(reporter); |
| tomhudson@google.com | 381010e | 2013-10-24 11:12:47 +0000 | [diff] [blame] | 1209 | test_hierarchical(reporter); |
| robertphillips@google.com | d550088 | 2014-04-02 23:51:13 +0000 | [diff] [blame] | 1210 | test_gen_id(reporter); |
| scroggo@google.com | d614c6a | 2012-09-14 17:26:37 +0000 | [diff] [blame] | 1211 | } |
| commit-bot@chromium.org | 50b393a | 2014-02-10 18:29:10 +0000 | [diff] [blame] | 1212 | |
| 1213 | static void draw_bitmaps(const SkBitmap bitmap, SkCanvas* canvas) { |
| 1214 | const SkPaint paint; |
| 1215 | const SkRect rect = { 5.0f, 5.0f, 8.0f, 8.0f }; |
| 1216 | const SkIRect irect = { 2, 2, 3, 3 }; |
| 1217 | |
| 1218 | // Don't care what these record, as long as they're legal. |
| 1219 | canvas->drawBitmap(bitmap, 0.0f, 0.0f, &paint); |
| 1220 | canvas->drawBitmapRectToRect(bitmap, &rect, rect, &paint, SkCanvas::kNone_DrawBitmapRectFlag); |
| 1221 | canvas->drawBitmapMatrix(bitmap, SkMatrix::I(), &paint); |
| 1222 | canvas->drawBitmapNine(bitmap, irect, rect, &paint); |
| 1223 | canvas->drawSprite(bitmap, 1, 1); |
| 1224 | } |
| 1225 | |
| 1226 | static void test_draw_bitmaps(SkCanvas* canvas) { |
| 1227 | SkBitmap empty; |
| 1228 | draw_bitmaps(empty, canvas); |
| mike@reedtribe.org | deee496 | 2014-02-13 14:41:43 +0000 | [diff] [blame] | 1229 | empty.setConfig(SkImageInfo::MakeN32Premul(10, 10)); |
| commit-bot@chromium.org | 50b393a | 2014-02-10 18:29:10 +0000 | [diff] [blame] | 1230 | draw_bitmaps(empty, canvas); |
| 1231 | } |
| 1232 | |
| 1233 | DEF_TEST(Picture_EmptyBitmap, r) { |
| robertphillips@google.com | 84b18c7 | 2014-04-13 19:09:42 +0000 | [diff] [blame] | 1234 | SkPictureRecorder recorder; |
| commit-bot@chromium.org | 5fb2ce3 | 2014-04-17 23:35:06 +0000 | [diff] [blame^] | 1235 | test_draw_bitmaps(recorder.beginRecording(10, 10, NULL, 0)); |
| robertphillips@google.com | 84b18c7 | 2014-04-13 19:09:42 +0000 | [diff] [blame] | 1236 | SkAutoTUnref<SkPicture> picture(recorder.endRecording()); |
| commit-bot@chromium.org | 50b393a | 2014-02-10 18:29:10 +0000 | [diff] [blame] | 1237 | } |
| 1238 | |
| 1239 | DEF_TEST(Canvas_EmptyBitmap, r) { |
| 1240 | SkBitmap dst; |
| mike@reedtribe.org | deee496 | 2014-02-13 14:41:43 +0000 | [diff] [blame] | 1241 | dst.allocN32Pixels(10, 10); |
| commit-bot@chromium.org | 50b393a | 2014-02-10 18:29:10 +0000 | [diff] [blame] | 1242 | SkCanvas canvas(dst); |
| 1243 | |
| 1244 | test_draw_bitmaps(&canvas); |
| 1245 | } |