RecordDrawTest.cpp revision c4b21e6c03a6cdb03e116b9f510eb10cf8daedb1
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 "Test.h" 9 10#include "SkDebugCanvas.h" 11#include "SkRecord.h" 12#include "SkRecordCulling.h" 13#include "SkRecordDraw.h" 14#include "SkRecorder.h" 15#include "SkRecords.h" 16 17static const int W = 1920, H = 1080; 18 19DEF_TEST(RecordDraw_Culling, r) { 20 // Record these 7 drawing commands verbatim. 21 SkRecord record; 22 SkRecorder recorder(SkRecorder::kWriteOnly_Mode, &record, W, H); 23 24 recorder.pushCull(SkRect::MakeWH(100, 100)); 25 recorder.drawRect(SkRect::MakeWH(10, 10), SkPaint()); 26 recorder.drawRect(SkRect::MakeWH(30, 30), SkPaint()); 27 recorder.pushCull(SkRect::MakeWH(5, 5)); 28 recorder.drawRect(SkRect::MakeWH(1, 1), SkPaint()); 29 recorder.popCull(); 30 recorder.popCull(); 31 32 // Take a pass over to match up pushCulls and popCulls. 33 SkRecordAnnotateCullingPairs(&record); 34 35 // Rerecord into another SkRecord using full SkCanvas semantics, 36 // tracking clips and allowing SkRecordDraw's quickReject() calls to work. 37 SkRecord rerecord; 38 SkRecorder rerecorder(SkRecorder::kReadWrite_Mode, &rerecord, W, H); 39 // This clip intersects the outer cull, but allows us to quick reject the inner one. 40 rerecorder.clipRect(SkRect::MakeLTRB(20, 20, 200, 200)); 41 42 SkRecordDraw(record, &rerecorder); 43 44 // We'll keep the clipRect call from above, and the outer two drawRects, and the push/pop pair. 45 // If culling weren't working, we'd see 8 commands recorded here. 46 REPORTER_ASSERT(r, 5 == rerecord.count()); 47} 48 49DEF_TEST(RecordDraw_Clipping, r) { 50 SkRecord record; 51 SkRecorder recorder(SkRecorder::kWriteOnly_Mode, &record, W, H); 52 53 // 9 draw commands. 54 recorder.save(); 55 recorder.clipRect(SkRect::MakeLTRB(0, 0, 100, 100)); 56 recorder.drawRect(SkRect::MakeLTRB(20, 20, 40, 40), SkPaint()); 57 recorder.save(); 58 // This first clipRect makes the clip empty, so the next two commands do nothing. 59 recorder.clipRect(SkRect::MakeLTRB(200, 200, 300, 300)); 60 recorder.clipRect(SkRect::MakeLTRB(210, 210, 250, 250)); // Skipped 61 recorder.drawRect(SkRect::MakeLTRB(220, 220, 240, 240), SkPaint()); // Skipped 62 recorder.restore(); 63 recorder.restore(); 64 65 // Same deal as above: we need full SkCanvas semantics for clip skipping to work. 66 SkRecord rerecord; 67 SkRecorder rerecorder(SkRecorder::kReadWrite_Mode, &rerecord, W, H); 68 SkRecordDraw(record, &rerecorder); 69 70 // All commands except the two marked // Skipped above will be preserved. 71 REPORTER_ASSERT(r, 7 == rerecord.count()); 72} 73