OLD | NEW |
(Empty) | |
| 1 /* |
| 2 * Copyright 2013 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 */ |
| 7 |
| 8 #include "gm.h" |
| 9 #include "..\debugger\SkDebugCanvas.h" |
| 10 #include "SkPictureFlat.h" |
| 11 |
| 12 namespace { |
| 13 |
| 14 // Do the commands in 'input' match the supplied pattern? Note: this is a pretty |
| 15 // heavy-weight operation since we are drawing the picture into a debug canvas |
| 16 // to extract the commands. |
| 17 bool check_pattern(SkPicture& input, const SkTDArray<DrawType> &pattern) { |
| 18 SkDebugCanvas debugCanvas(input.width(), input.height()); |
| 19 debugCanvas.setBounds(input.width(), input.height()); |
| 20 input.draw(&debugCanvas); |
| 21 |
| 22 if (pattern.count() != debugCanvas.getSize()) { |
| 23 return false; |
| 24 } |
| 25 |
| 26 for (int i = 0; i < pattern.count(); ++i) { |
| 27 if (pattern[i] != debugCanvas.getDrawCommandAt(i)->getType()) { |
| 28 return false; |
| 29 } |
| 30 } |
| 31 |
| 32 return true; |
| 33 } |
| 34 |
| 35 // construct the pattern removed by the SkPictureRecord::remove_save_layer1 |
| 36 // optimization, i.e.: |
| 37 // SAVE_LAYER |
| 38 // DRAW_BITMAP|DRAW_BITMAP_MATRIX|DRAW_BITMAP_NINE|DRAW_BITMAP_RECT_TO_REC
T |
| 39 // RESTORE |
| 40 SkPicture* create_save_layer_opt_1(SkTDArray<DrawType> *preOptPattern, |
| 41 SkTDArray<DrawType> *postOptPattern, |
| 42 const SkBitmap& checkerBoard) { |
| 43 // Create the pattern that should trigger the optimization |
| 44 preOptPattern->setCount(5); |
| 45 (*preOptPattern)[0] = SAVE; |
| 46 (*preOptPattern)[1] = SAVE_LAYER; |
| 47 (*preOptPattern)[2] = DRAW_BITMAP_RECT_TO_RECT; |
| 48 (*preOptPattern)[3] = RESTORE; |
| 49 (*preOptPattern)[4] = RESTORE; |
| 50 |
| 51 // Create the pattern that should appear after the optimization |
| 52 postOptPattern->setCount(5); |
| 53 (*postOptPattern)[0] = SAVE; // extra save/restore added by extra draw |
| 54 (*postOptPattern)[1] = SAVE; |
| 55 (*postOptPattern)[2] = DRAW_BITMAP_RECT_TO_RECT; |
| 56 (*postOptPattern)[3] = RESTORE; |
| 57 (*postOptPattern)[4] = RESTORE; |
| 58 |
| 59 SkPicture* result = new SkPicture; |
| 60 |
| 61 // have to disable the optimizations while generating the picture |
| 62 SkCanvas* canvas = result->beginRecording(100, 100, |
| 63 SkPicture::kDisableOptimizations_RecordingFlag); |
| 64 |
| 65 SkPaint saveLayerPaint; |
| 66 saveLayerPaint.setColor(0xCC000000); |
| 67 |
| 68 // saveLayer's 'bounds' parameter must be NULL for this optimization |
| 69 canvas->saveLayer(NULL, &saveLayerPaint); |
| 70 |
| 71 SkRect rect = { 10, 10, 90, 90 }; |
| 72 |
| 73 // The dbmr2r's paint must be opaque |
| 74 SkPaint dbmr2rPaint; |
| 75 dbmr2rPaint.setColor(0xFF000000); |
| 76 |
| 77 canvas->drawBitmapRectToRect(checkerBoard, NULL, rect, &dbmr2rPaint); |
| 78 canvas->restore(); |
| 79 |
| 80 result->endRecording(); |
| 81 |
| 82 return result; |
| 83 } |
| 84 |
| 85 // construct the pattern removed by the SkPictureRecord::remove_save_layer2 |
| 86 // optimization, i.e.: |
| 87 // SAVE_LAYER (with NULL == bounds) |
| 88 // SAVE |
| 89 // CLIP_RECT |
| 90 // DRAW_BITMAP|DRAW_BITMAP_MATRIX|DRAW_BITMAP_NINE|DRAW_BITMAP_RECT_TO_R
ECT |
| 91 // RESTORE |
| 92 // RESTORE |
| 93 SkPicture* create_save_layer_opt_2(SkTDArray<DrawType> *preOptPattern, |
| 94 SkTDArray<DrawType> *postOptPattern, |
| 95 const SkBitmap& checkerBoard) { |
| 96 // Create the pattern that should trigger the optimization |
| 97 preOptPattern->setCount(8); |
| 98 (*preOptPattern)[0] = SAVE; |
| 99 (*preOptPattern)[1] = SAVE_LAYER; |
| 100 (*preOptPattern)[2] = SAVE; |
| 101 (*preOptPattern)[3] = CLIP_RECT; |
| 102 (*preOptPattern)[4] = DRAW_BITMAP_RECT_TO_RECT; |
| 103 (*preOptPattern)[5] = RESTORE; |
| 104 (*preOptPattern)[6] = RESTORE; |
| 105 (*preOptPattern)[7] = RESTORE; |
| 106 |
| 107 // Create the pattern that should appear after the optimization |
| 108 postOptPattern->setCount(8); |
| 109 (*postOptPattern)[0] = SAVE; // extra save/restore added by extra draw |
| 110 (*postOptPattern)[1] = SAVE; |
| 111 (*postOptPattern)[2] = SAVE; |
| 112 (*postOptPattern)[3] = CLIP_RECT; |
| 113 (*postOptPattern)[4] = DRAW_BITMAP_RECT_TO_RECT; |
| 114 (*postOptPattern)[5] = RESTORE; |
| 115 (*postOptPattern)[6] = RESTORE; |
| 116 (*postOptPattern)[7] = RESTORE; |
| 117 |
| 118 SkPicture* result = new SkPicture; |
| 119 |
| 120 // have to disable the optimizations while generating the picture |
| 121 SkCanvas* canvas = result->beginRecording(100, 100, |
| 122 SkPicture::kDisableOptimizations_RecordingFlag); |
| 123 |
| 124 SkPaint saveLayerPaint; |
| 125 saveLayerPaint.setColor(0xCC000000); |
| 126 |
| 127 // saveLayer's 'bounds' parameter must be NULL for this optimization |
| 128 canvas->saveLayer(NULL, &saveLayerPaint); |
| 129 |
| 130 canvas->save(); |
| 131 |
| 132 SkRect rect = { 10, 10, 90, 90 }; |
| 133 canvas->clipRect(rect); |
| 134 |
| 135 // The dbmr2r's paint must be opaque |
| 136 SkPaint dbmr2rPaint; |
| 137 dbmr2rPaint.setColor(0xFF000000); |
| 138 |
| 139 canvas->drawBitmapRectToRect(checkerBoard, NULL, rect, &dbmr2rPaint); |
| 140 canvas->restore(); |
| 141 canvas->restore(); |
| 142 |
| 143 result->endRecording(); |
| 144 |
| 145 return result; |
| 146 } |
| 147 |
| 148 }; |
| 149 |
| 150 |
| 151 // As our .skp optimizations get folded into the captured skps our code will |
| 152 // no longer be locally exercised. This GM manually constructs the patterns |
| 153 // our optimizations will remove to test them. It acts as both a GM and a unit |
| 154 // test |
| 155 class OptimizationsGM : public skiagm::GM { |
| 156 public: |
| 157 OptimizationsGM() { |
| 158 this->makeCheckerboard(); |
| 159 } |
| 160 |
| 161 protected: |
| 162 SkString onShortName() { |
| 163 return SkString("optimizations"); |
| 164 } |
| 165 |
| 166 SkISize onISize() { return SkISize::Make(800, 800); } |
| 167 |
| 168 typedef SkPicture* (*PFCreateOpt)(SkTDArray<DrawType> *preOptPattern, |
| 169 SkTDArray<DrawType> *postOptPattern, |
| 170 const SkBitmap& checkerBoard); |
| 171 |
| 172 virtual void onDraw(SkCanvas* canvas) { |
| 173 |
| 174 PFCreateOpt gOpts[] = { |
| 175 create_save_layer_opt_1, |
| 176 create_save_layer_opt_2 |
| 177 }; |
| 178 |
| 179 SkTDArray<DrawType> prePattern, postPattern; |
| 180 |
| 181 for (int i = 0; i < SK_ARRAY_COUNT(gOpts); ++i) { |
| 182 SkAutoTUnref<SkPicture> pre((*gOpts)(&prePattern, &postPattern, fChe
ckerboard)); |
| 183 |
| 184 SkASSERT(check_pattern(*pre, prePattern)); |
| 185 |
| 186 canvas->drawPicture(*pre); |
| 187 |
| 188 canvas->translate(SkIntToScalar(pre->width()), 0); |
| 189 |
| 190 SkAutoTUnref<SkPicture> post(new SkPicture); |
| 191 |
| 192 SkCanvas* recordCanvas = post->beginRecording(pre->width(), pre->hei
ght()); |
| 193 |
| 194 pre->draw(recordCanvas); |
| 195 |
| 196 post->endRecording(); |
| 197 |
| 198 SkASSERT(check_pattern(*post, postPattern)); |
| 199 |
| 200 canvas->drawPicture(*post); |
| 201 |
| 202 canvas->translate(SkIntToScalar(-pre->width()), |
| 203 SkIntToScalar(post->height())); |
| 204 |
| 205 // TODO: we could also render the pre and post pictures to bitmaps |
| 206 // and manually compare them in this method |
| 207 } |
| 208 } |
| 209 |
| 210 private: |
| 211 void makeCheckerboard() { |
| 212 static const unsigned int kCheckerboardWidth = 16; |
| 213 static const unsigned int kCheckerboardHeight = 16; |
| 214 |
| 215 fCheckerboard.setConfig(SkBitmap::kARGB_8888_Config, |
| 216 kCheckerboardWidth, kCheckerboardHeight); |
| 217 fCheckerboard.allocPixels(); |
| 218 SkAutoLockPixels lock(fCheckerboard); |
| 219 for (int y = 0; y < kCheckerboardHeight; y += 2) { |
| 220 SkPMColor* scanline = fCheckerboard.getAddr32(0, y); |
| 221 for (int x = 0; x < kCheckerboardWidth; x += 2) { |
| 222 *scanline++ = 0xFFFFFFFF; |
| 223 *scanline++ = 0xFF000000; |
| 224 } |
| 225 scanline = fCheckerboard.getAddr32(0, y + 1); |
| 226 for (int x = 0; x < kCheckerboardWidth; x += 2) { |
| 227 *scanline++ = 0xFF000000; |
| 228 *scanline++ = 0xFFFFFFFF; |
| 229 } |
| 230 } |
| 231 } |
| 232 |
| 233 SkBitmap fCheckerboard; |
| 234 |
| 235 typedef skiagm::GM INHERITED; |
| 236 }; |
| 237 |
| 238 ////////////////////////////////////////////////////////////////////////////// |
| 239 |
| 240 DEF_GM( return new OptimizationsGM; ) |
OLD | NEW |