2 * Copyright 2014 Google Inc.
4 * Use of this source code is governed by a BSD-style license that can be
5 * found in the LICENSE file.
11 #include "SkImageInfo.h"
14 #include "SkRecordAnalysis.h"
15 #include "SkRecords.h"
17 // Sums the area of any DrawRect command it sees.
20 AreaSummer() : fArea(0) {}
22 template <typename T> void operator()(const T&) { }
24 void operator()(const SkRecords::DrawRect& draw) {
25 fArea += (int)(draw.rect.width() * draw.rect.height());
28 int area() const { return fArea; }
30 void apply(const SkRecord& record) {
31 for (unsigned i = 0; i < record.count(); i++) {
32 record.visit<void>(i, *this);
40 // Scales out the bottom-right corner of any DrawRect command it sees by 2x.
42 template <typename T> void operator()(T*) {}
43 void operator()(SkRecords::DrawRect* draw) {
44 draw->rect.fRight *= 2;
45 draw->rect.fBottom *= 2;
48 void apply(SkRecord* record) {
49 for (unsigned i = 0; i < record->count(); i++) {
50 record->mutate<void>(i, *this);
55 #define APPEND(record, type, ...) SkNEW_PLACEMENT_ARGS(record.append<type>(), type, (__VA_ARGS__))
57 // Basic tests for the low-level SkRecord code.
61 // Add a simple DrawRect command.
62 SkRect rect = SkRect::MakeWH(10, 10);
64 APPEND(record, SkRecords::DrawRect, paint, rect);
66 // Its area should be 100.
69 REPORTER_ASSERT(r, summer.area() == 100);
73 stretch.apply(&record);
75 // Now its area should be 100 + 400.
77 REPORTER_ASSERT(r, summer.area() == 500);
80 DEF_TEST(RecordAnalysis, r) {
83 SkRect rect = SkRect::MakeWH(10, 10);
85 APPEND(record, SkRecords::DrawRect, paint, rect);
86 REPORTER_ASSERT(r, !SkRecordWillPlaybackBitmaps(record));
89 APPEND(record, SkRecords::DrawBitmap, &paint, bitmap, 0.0f, 0.0f);
90 REPORTER_ASSERT(r, SkRecordWillPlaybackBitmaps(record));
92 SkNEW_PLACEMENT_ARGS(record.replace<SkRecords::DrawRect>(1),
93 SkRecords::DrawRect, (paint, rect));
94 REPORTER_ASSERT(r, !SkRecordWillPlaybackBitmaps(record));
97 // CreateBitmapShader is too smart for us; an empty (or 1x1) bitmap shader
98 // gets optimized into a non-bitmap form, so we create a 2x2 bitmap here.
100 bitmap2.allocPixels(SkImageInfo::MakeN32Premul(2, 2));
101 bitmap2.eraseColor(SK_ColorBLUE);
102 *(bitmap2.getAddr32(0, 0)) = SK_ColorGREEN;
103 SkShader* shader = SkShader::CreateBitmapShader(bitmap2, SkShader::kClamp_TileMode,
104 SkShader::kClamp_TileMode);
105 paint2.setShader(shader)->unref();
106 REPORTER_ASSERT(r, shader->asABitmap(NULL, NULL, NULL) == SkShader::kDefault_BitmapType);
108 APPEND(record, SkRecords::DrawRect, paint2, rect);
109 REPORTER_ASSERT(r, SkRecordWillPlaybackBitmaps(record));