Home | History | Annotate | Download | only in tests
      1 /*
      2  * Copyright 2014 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 "SkBBHFactory.h"
      9 #include "SkCanvas.h"
     10 #include "SkPictureRecorder.h"
     11 #include "SkPictureStateTree.h"
     12 #include "Test.h"
     13 
     14 static SkPicture* draw_scene(SkBBHFactory* bbhFactory) {
     15     SkPictureRecorder recorder;
     16     SkCanvas* canvas = recorder.beginRecording(200, 200, bbhFactory, 0);
     17 
     18     SkPaint p1, p2;
     19     p1.setStyle(SkPaint::kFill_Style);
     20     p1.setARGB(0x80, 0, 0xff, 0);
     21     p2.setStyle(SkPaint::kFill_Style);
     22     p2.setARGB(0x80, 0xff, 0, 0);
     23 
     24     canvas->drawColor(SK_ColorWHITE);
     25 
     26     // This is intended to exercise some tricky SkPictureStateTree code paths when
     27     // played back with various clips:
     28     //
     29     //   * cleanup/rewind when the last draw is not at the root of the state tree.
     30     //   * state nodes with both kSave_Flag & kSaveLayer_Flag set.
     31     //   * state tree transitions which implicitly reset the matrix via. restore().
     32 
     33     canvas->save();
     34       canvas->translate(10, 10);
     35 
     36       canvas->drawRect(SkRect::MakeWH(100, 50), p1);
     37       canvas->drawRect(SkRect::MakeWH(50, 100), p2);
     38 
     39       SkRect layerBounds = SkRect::MakeXYWH(0, 0, 90, 90);
     40       canvas->saveLayer(&layerBounds, NULL);
     41         canvas->save();
     42           canvas->clipRect(layerBounds);
     43 
     44           canvas->save();
     45             canvas->clipRect(SkRect::MakeWH(25, 25));
     46             canvas->rotate(90);
     47             canvas->drawRect(SkRect::MakeWH(100, 50), p1);
     48           canvas->restore();
     49 
     50           canvas->save();
     51             canvas->clipRect(SkRect::MakeWH(25, 25));
     52             canvas->save();
     53               canvas->rotate(90);
     54               canvas->drawRect(SkRect::MakeWH(50, 100), p2);
     55             canvas->restore();
     56             canvas->drawRect(SkRect::MakeWH(100, 50), p1);
     57           canvas->restore();
     58           canvas->drawRect(SkRect::MakeXYWH(99, 99, 1, 1), p1);
     59         canvas->restore();
     60       canvas->restore();
     61 
     62     canvas->restore();
     63 
     64     return recorder.endRecording();
     65 }
     66 
     67 static void check_bms(skiatest::Reporter* reporter, const SkBitmap& bm1, const SkBitmap& bm2) {
     68     SkASSERT(bm1.getSize() == bm2.getSize());
     69     REPORTER_ASSERT(reporter, 0 == memcmp(bm1.getAddr(0, 0), bm2.getAddr(0, 0), bm1.getSize()));
     70 }
     71 
     72 static void test_reference_picture(skiatest::Reporter* reporter) {
     73     SkRTreeFactory bbhFactory;
     74 
     75     SkAutoTUnref<SkPicture> bbhPicture(draw_scene(&bbhFactory));
     76     SkAutoTUnref<SkPicture> referencePicture(draw_scene(NULL));
     77 
     78     SkBitmap referenceBitmap;
     79     referenceBitmap.allocN32Pixels(100, 100);
     80     SkCanvas referenceCanvas(referenceBitmap);
     81 
     82     SkBitmap bbhBitmap;
     83     bbhBitmap.allocN32Pixels(100, 100);
     84     SkCanvas bbhCanvas(bbhBitmap);
     85 
     86     referenceCanvas.drawColor(SK_ColorTRANSPARENT);
     87     referenceCanvas.drawPicture(referencePicture.get());
     88     bbhCanvas.drawColor(SK_ColorTRANSPARENT);
     89     bbhCanvas.drawPicture(bbhPicture.get());
     90     REPORTER_ASSERT(reporter,
     91                     referenceCanvas.getSaveCount() == bbhCanvas.getSaveCount());
     92     REPORTER_ASSERT(reporter,
     93                     referenceCanvas.getTotalMatrix() == bbhCanvas.getTotalMatrix());
     94     check_bms(reporter, referenceBitmap, bbhBitmap);
     95 
     96     referenceCanvas.drawColor(SK_ColorTRANSPARENT);
     97     referenceCanvas.clipRect(SkRect::MakeWH(50, 50));
     98     referenceCanvas.drawPicture(referencePicture.get());
     99     bbhCanvas.drawColor(SK_ColorTRANSPARENT);
    100     bbhCanvas.clipRect(SkRect::MakeWH(50, 50));
    101     bbhCanvas.drawPicture(bbhPicture.get());
    102     REPORTER_ASSERT(reporter,
    103                     referenceCanvas.getSaveCount() == bbhCanvas.getSaveCount());
    104     REPORTER_ASSERT(reporter,
    105                     referenceCanvas.getTotalMatrix() == bbhCanvas.getTotalMatrix());
    106     check_bms(reporter, referenceBitmap, bbhBitmap);
    107 
    108     referenceCanvas.drawColor(SK_ColorTRANSPARENT);
    109     referenceCanvas.clipRect(SkRect::MakeWH(10, 10));
    110     referenceCanvas.drawPicture(referencePicture.get());
    111     bbhCanvas.drawColor(SK_ColorTRANSPARENT);
    112     bbhCanvas.clipRect(SkRect::MakeWH(10, 10));
    113     bbhCanvas.drawPicture(bbhPicture.get());
    114     REPORTER_ASSERT(reporter,
    115                     referenceCanvas.getSaveCount() == bbhCanvas.getSaveCount());
    116     REPORTER_ASSERT(reporter,
    117                     referenceCanvas.getTotalMatrix() == bbhCanvas.getTotalMatrix());
    118     check_bms(reporter, referenceBitmap, bbhBitmap);
    119 }
    120 
    121 DEF_TEST(PictureStateTree, reporter) {
    122     test_reference_picture(reporter);
    123 }
    124