| OLD | NEW |
| 1 /* | 1 /* |
| 2 * Copyright 2013 Google Inc. | 2 * Copyright 2013 Google Inc. |
| 3 * | 3 * |
| 4 * Use of this source code is governed by a BSD-style license that can be | 4 * Use of this source code is governed by a BSD-style license that can be |
| 5 * found in the LICENSE file. | 5 * found in the LICENSE file. |
| 6 */ | 6 */ |
| 7 | 7 |
| 8 #include "gm.h" | 8 #include "gm.h" |
| 9 #include "SkDebugCanvas.h" | 9 #include "SkDebugCanvas.h" |
| 10 #include "SkPictureFlat.h" | 10 #include "SkPictureFlat.h" |
| 11 #include "SkPictureRecorder.h" | 11 #include "SkPictureRecorder.h" |
| 12 | 12 |
| 13 #define WARN(msg) \ | 13 #define WARN(msg) \ |
| 14 SkDebugf("%s:%d: %s\n", __FILE__, __LINE__, msg); | 14 SkDebugf("%s:%d: %s\n", __FILE__, __LINE__, msg); |
| 15 | 15 |
| 16 // Do the commands in 'input' match the supplied pattern? Note: this is a pretty | 16 // Do the commands in 'input' match the supplied pattern? Note: this is a pretty |
| 17 // heavy-weight operation since we are drawing the picture into a debug canvas | 17 // heavy-weight operation since we are drawing the picture into a debug canvas |
| 18 // to extract the commands. | 18 // to extract the commands. |
| 19 static bool check_pattern(SkPicture& input, const SkTDArray<DrawType> &pattern)
{ | 19 static bool check_pattern(SkPicture& input, const SkTDArray<DrawType> &pattern)
{ |
| 20 SkDebugCanvas debugCanvas(input.width(), input.height()); | 20 SkDebugCanvas debugCanvas(SkScalarCeilToInt(input.cullRect().width()), |
| 21 debugCanvas.setBounds(input.width(), input.height()); | 21 SkScalarCeilToInt(input.cullRect().height())); |
| 22 input.draw(&debugCanvas); | 22 input.draw(&debugCanvas); |
| 23 | 23 |
| 24 if (pattern.count() != debugCanvas.getSize()) { | 24 if (pattern.count() != debugCanvas.getSize()) { |
| 25 return false; | 25 return false; |
| 26 } | 26 } |
| 27 | 27 |
| 28 for (int i = 0; i < pattern.count(); ++i) { | 28 for (int i = 0; i < pattern.count(); ++i) { |
| 29 if (pattern[i] != debugCanvas.getDrawCommandAt(i)->getType()) { | 29 if (pattern[i] != debugCanvas.getDrawCommandAt(i)->getType()) { |
| 30 return false; | 30 return false; |
| 31 } | 31 } |
| (...skipping 301 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 333 create_save_layer_opt_1_v2, | 333 create_save_layer_opt_1_v2, |
| 334 create_save_layer_opt_1_v3, | 334 create_save_layer_opt_1_v3, |
| 335 create_save_layer_opt_1_v4, | 335 create_save_layer_opt_1_v4, |
| 336 create_save_layer_opt_2_v1, | 336 create_save_layer_opt_2_v1, |
| 337 create_save_layer_opt_2_v2, | 337 create_save_layer_opt_2_v2, |
| 338 create_save_layer_opt_2_v3, | 338 create_save_layer_opt_2_v3, |
| 339 create_save_layer_opt_2_v4, | 339 create_save_layer_opt_2_v4, |
| 340 }; | 340 }; |
| 341 | 341 |
| 342 SkTDArray<DrawType> prePattern, postPattern; | 342 SkTDArray<DrawType> prePattern, postPattern; |
| 343 int xPos = 0, yPos = 0; | 343 SkScalar xPos = 0, yPos = 0; |
| 344 | 344 |
| 345 for (size_t i = 0; i < SK_ARRAY_COUNT(gOpts); ++i) { | 345 for (size_t i = 0; i < SK_ARRAY_COUNT(gOpts); ++i) { |
| 346 SkAutoTUnref<SkPicture> pre((*gOpts[i])(&prePattern, &postPattern, f
Checkerboard)); | 346 SkAutoTUnref<SkPicture> pre((*gOpts[i])(&prePattern, &postPattern, f
Checkerboard)); |
| 347 | 347 |
| 348 if (!(check_pattern(*pre, prePattern))) { | 348 if (!(check_pattern(*pre, prePattern))) { |
| 349 WARN("Pre optimization pattern mismatch"); | 349 WARN("Pre optimization pattern mismatch"); |
| 350 SkASSERT(0); | 350 SkASSERT(0); |
| 351 } | 351 } |
| 352 | 352 |
| 353 canvas->save(); | 353 canvas->save(); |
| 354 canvas->translate(SkIntToScalar(xPos), SkIntToScalar(yPos)); | 354 canvas->translate(xPos, yPos); |
| 355 pre->draw(canvas); | 355 pre->draw(canvas); |
| 356 xPos += pre->width(); | 356 xPos += pre->cullRect().width(); |
| 357 canvas->restore(); | 357 canvas->restore(); |
| 358 | 358 |
| 359 // re-render the 'pre' picture and thus 'apply' the optimization | 359 // re-render the 'pre' picture and thus 'apply' the optimization |
| 360 SkPictureRecorder recorder; | 360 SkPictureRecorder recorder; |
| 361 | 361 |
| 362 SkCanvas* recordCanvas = | 362 SkCanvas* recordCanvas = |
| 363 recorder.DEPRECATED_beginRecording(pre->width(), pre->height(),
NULL, 0); | 363 recorder.DEPRECATED_beginRecording(pre->cullRect().width(), |
| 364 pre->cullRect().height(), |
| 365 NULL, 0); |
| 364 | 366 |
| 365 pre->draw(recordCanvas); | 367 pre->draw(recordCanvas); |
| 366 | 368 |
| 367 SkAutoTUnref<SkPicture> post(recorder.endRecording()); | 369 SkAutoTUnref<SkPicture> post(recorder.endRecording()); |
| 368 | 370 |
| 369 if (!(check_pattern(*post, postPattern))) { | 371 if (!(check_pattern(*post, postPattern))) { |
| 370 WARN("Post optimization pattern mismatch"); | 372 WARN("Post optimization pattern mismatch"); |
| 371 SkASSERT(0); | 373 SkASSERT(0); |
| 372 } | 374 } |
| 373 | 375 |
| 374 canvas->save(); | 376 canvas->save(); |
| 375 canvas->translate(SkIntToScalar(xPos), SkIntToScalar(yPos)); | 377 canvas->translate(xPos, yPos); |
| 376 post->draw(canvas); | 378 post->draw(canvas); |
| 377 xPos += post->width(); | 379 xPos += post->cullRect().width(); |
| 378 canvas->restore(); | 380 canvas->restore(); |
| 379 | 381 |
| 380 if (xPos >= kWidth) { | 382 if (xPos >= kWidth) { |
| 381 // start a new line | 383 // start a new line |
| 382 xPos = 0; | 384 xPos = 0; |
| 383 yPos += post->height(); | 385 yPos += post->cullRect().height(); |
| 384 } | 386 } |
| 385 | 387 |
| 386 // TODO: we could also render the pre and post pictures to bitmaps | 388 // TODO: we could also render the pre and post pictures to bitmaps |
| 387 // and manually compare them in this method | 389 // and manually compare them in this method |
| 388 } | 390 } |
| 389 } | 391 } |
| 390 | 392 |
| 391 private: | 393 private: |
| 392 void makeCheckerboard() { | 394 void makeCheckerboard() { |
| 393 static const unsigned int kCheckerboardWidth = 16; | 395 static const unsigned int kCheckerboardWidth = 16; |
| (...skipping 15 matching lines...) Expand all Loading... |
| 409 } | 411 } |
| 410 | 412 |
| 411 SkBitmap fCheckerboard; | 413 SkBitmap fCheckerboard; |
| 412 | 414 |
| 413 typedef skiagm::GM INHERITED; | 415 typedef skiagm::GM INHERITED; |
| 414 }; | 416 }; |
| 415 | 417 |
| 416 ////////////////////////////////////////////////////////////////////////////// | 418 ////////////////////////////////////////////////////////////////////////////// |
| 417 | 419 |
| 418 DEF_GM( return new OptimizationsGM; ) | 420 DEF_GM( return new OptimizationsGM; ) |
| OLD | NEW |