blob: d830aea6e563c2b2b9c6441d0975a7e7102eb3c9 [file] [log] [blame]
commit-bot@chromium.orgc4b21e62014-04-11 18:33:31 +00001/*
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
commit-bot@chromium.orgd9ce2be2014-04-09 23:30:28 +00008#include "Test.h"
9
10#include "SkDebugCanvas.h"
11#include "SkRecord.h"
commit-bot@chromium.orgad8ce572014-04-21 15:03:36 +000012#include "SkRecordOpts.h"
commit-bot@chromium.orgd9ce2be2014-04-09 23:30:28 +000013#include "SkRecordDraw.h"
14#include "SkRecorder.h"
15#include "SkRecords.h"
16
17static const int W = 1920, H = 1080;
18
commit-bot@chromium.org8dac8b12014-04-30 13:18:12 +000019static void draw_pos_text_h(SkCanvas* canvas, const char* text, SkScalar y) {
20 const size_t len = strlen(text);
21 SkAutoTMalloc<SkScalar> xpos(len);
22 for (size_t i = 0; i < len; i++) {
23 xpos[i] = (SkScalar)i;
24 }
25 canvas->drawPosTextH(text, len, xpos, y, SkPaint());
26}
27
28// Rerecord into another SkRecord using full SkCanvas semantics,
29// tracking clips and allowing SkRecordDraw's quickReject() calls to work.
30static void record_clipped(const SkRecord& record, SkRect clip, SkRecord* clipped) {
31 SkRecorder recorder(SkRecorder::kReadWrite_Mode, clipped, W, H);
32 recorder.clipRect(clip);
33 SkRecordDraw(record, &recorder);
34}
35
36DEF_TEST(RecordDraw_PosTextHQuickReject, r) {
37 SkRecord record;
38 SkRecorder recorder(SkRecorder::kWriteOnly_Mode, &record, W, H);
39
40 draw_pos_text_h(&recorder, "This will draw.", 20);
41 draw_pos_text_h(&recorder, "This won't.", 5000);
42
43 SkRecordBoundDrawPosTextH(&record);
44
45 SkRecord clipped;
46 record_clipped(record, SkRect::MakeLTRB(20, 20, 200, 200), &clipped);
47
48 // clipRect and the first drawPosTextH.
49 REPORTER_ASSERT(r, 2 == clipped.count());
50}
51
commit-bot@chromium.orgd9ce2be2014-04-09 23:30:28 +000052DEF_TEST(RecordDraw_Culling, r) {
53 // Record these 7 drawing commands verbatim.
54 SkRecord record;
55 SkRecorder recorder(SkRecorder::kWriteOnly_Mode, &record, W, H);
56
57 recorder.pushCull(SkRect::MakeWH(100, 100));
58 recorder.drawRect(SkRect::MakeWH(10, 10), SkPaint());
59 recorder.drawRect(SkRect::MakeWH(30, 30), SkPaint());
60 recorder.pushCull(SkRect::MakeWH(5, 5));
61 recorder.drawRect(SkRect::MakeWH(1, 1), SkPaint());
62 recorder.popCull();
63 recorder.popCull();
64
65 // Take a pass over to match up pushCulls and popCulls.
66 SkRecordAnnotateCullingPairs(&record);
67
commit-bot@chromium.orgd9ce2be2014-04-09 23:30:28 +000068 // This clip intersects the outer cull, but allows us to quick reject the inner one.
commit-bot@chromium.org8dac8b12014-04-30 13:18:12 +000069 SkRecord clipped;
70 record_clipped(record, SkRect::MakeLTRB(20, 20, 200, 200), &clipped);
commit-bot@chromium.orgd9ce2be2014-04-09 23:30:28 +000071
72 // We'll keep the clipRect call from above, and the outer two drawRects, and the push/pop pair.
73 // If culling weren't working, we'd see 8 commands recorded here.
commit-bot@chromium.org8dac8b12014-04-30 13:18:12 +000074 REPORTER_ASSERT(r, 5 == clipped.count());
commit-bot@chromium.orgd9ce2be2014-04-09 23:30:28 +000075}