blob: 2fcc1e9c53cc6a6020fa4aab979e6bb6ec91a015 [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.org066a28d2014-04-08 17:31:08 +00008#include "Test.h"
9
tomhudsond9305112014-07-05 13:37:53 -070010#include "SkBitmap.h"
11#include "SkImageInfo.h"
12#include "SkShader.h"
commit-bot@chromium.org066a28d2014-04-08 17:31:08 +000013#include "SkRecord.h"
tomhudsond9305112014-07-05 13:37:53 -070014#include "SkRecordAnalysis.h"
commit-bot@chromium.org066a28d2014-04-08 17:31:08 +000015#include "SkRecords.h"
16
commit-bot@chromium.org506db0b2014-04-08 23:31:35 +000017// Sums the area of any DrawRect command it sees.
commit-bot@chromium.org066a28d2014-04-08 17:31:08 +000018class AreaSummer {
19public:
commit-bot@chromium.org506db0b2014-04-08 23:31:35 +000020 AreaSummer() : fArea(0) {}
commit-bot@chromium.org066a28d2014-04-08 17:31:08 +000021
22 template <typename T> void operator()(const T&) { }
23
commit-bot@chromium.orgc71da1f2014-05-07 21:16:09 +000024 void operator()(const SkRecords::DrawRect& draw) {
25 fArea += (int)(draw.rect.width() * draw.rect.height());
26 }
27
commit-bot@chromium.org506db0b2014-04-08 23:31:35 +000028 int area() const { return fArea; }
29
commit-bot@chromium.org88c3e272014-04-22 16:57:20 +000030 void apply(const SkRecord& record) {
31 for (unsigned i = 0; i < record.count(); i++) {
commit-bot@chromium.orgc71da1f2014-05-07 21:16:09 +000032 record.visit<void>(i, *this);
commit-bot@chromium.org88c3e272014-04-22 16:57:20 +000033 }
34 }
35
commit-bot@chromium.org066a28d2014-04-08 17:31:08 +000036private:
commit-bot@chromium.org506db0b2014-04-08 23:31:35 +000037 int fArea;
commit-bot@chromium.org066a28d2014-04-08 17:31:08 +000038};
commit-bot@chromium.org066a28d2014-04-08 17:31:08 +000039
40// Scales out the bottom-right corner of any DrawRect command it sees by 2x.
41struct Stretch {
42 template <typename T> void operator()(T*) {}
commit-bot@chromium.orgc71da1f2014-05-07 21:16:09 +000043 void operator()(SkRecords::DrawRect* draw) {
44 draw->rect.fRight *= 2;
45 draw->rect.fBottom *= 2;
46 }
commit-bot@chromium.org88c3e272014-04-22 16:57:20 +000047
48 void apply(SkRecord* record) {
49 for (unsigned i = 0; i < record->count(); i++) {
commit-bot@chromium.orgc71da1f2014-05-07 21:16:09 +000050 record->mutate<void>(i, *this);
commit-bot@chromium.org88c3e272014-04-22 16:57:20 +000051 }
52 }
commit-bot@chromium.org066a28d2014-04-08 17:31:08 +000053};
commit-bot@chromium.org066a28d2014-04-08 17:31:08 +000054
tomhudsond9305112014-07-05 13:37:53 -070055#define APPEND(record, type, ...) SkNEW_PLACEMENT_ARGS(record.append<type>(), type, (__VA_ARGS__))
56
commit-bot@chromium.org066a28d2014-04-08 17:31:08 +000057// Basic tests for the low-level SkRecord code.
58DEF_TEST(Record, r) {
59 SkRecord record;
60
61 // Add a simple DrawRect command.
62 SkRect rect = SkRect::MakeWH(10, 10);
63 SkPaint paint;
tomhudsond9305112014-07-05 13:37:53 -070064 APPEND(record, SkRecords::DrawRect, paint, rect);
commit-bot@chromium.org066a28d2014-04-08 17:31:08 +000065
66 // Its area should be 100.
commit-bot@chromium.org506db0b2014-04-08 23:31:35 +000067 AreaSummer summer;
commit-bot@chromium.org88c3e272014-04-22 16:57:20 +000068 summer.apply(record);
commit-bot@chromium.org506db0b2014-04-08 23:31:35 +000069 REPORTER_ASSERT(r, summer.area() == 100);
commit-bot@chromium.org066a28d2014-04-08 17:31:08 +000070
commit-bot@chromium.org506db0b2014-04-08 23:31:35 +000071 // Scale 2x.
72 Stretch stretch;
commit-bot@chromium.org88c3e272014-04-22 16:57:20 +000073 stretch.apply(&record);
commit-bot@chromium.org506db0b2014-04-08 23:31:35 +000074
75 // Now its area should be 100 + 400.
commit-bot@chromium.org88c3e272014-04-22 16:57:20 +000076 summer.apply(record);
commit-bot@chromium.org506db0b2014-04-08 23:31:35 +000077 REPORTER_ASSERT(r, summer.area() == 500);
commit-bot@chromium.org066a28d2014-04-08 17:31:08 +000078}
tomhudsond9305112014-07-05 13:37:53 -070079
80DEF_TEST(RecordAnalysis, r) {
81 SkRecord record;
82
83 SkRect rect = SkRect::MakeWH(10, 10);
84 SkPaint paint;
85 APPEND(record, SkRecords::DrawRect, paint, rect);
86 REPORTER_ASSERT(r, !SkRecordWillPlaybackBitmaps(record));
87
88 SkBitmap bitmap;
89 APPEND(record, SkRecords::DrawBitmap, &paint, bitmap, 0.0f, 0.0f);
90 REPORTER_ASSERT(r, SkRecordWillPlaybackBitmaps(record));
91
92 SkNEW_PLACEMENT_ARGS(record.replace<SkRecords::DrawRect>(1),
93 SkRecords::DrawRect, (paint, rect));
94 REPORTER_ASSERT(r, !SkRecordWillPlaybackBitmaps(record));
95
96 SkPaint paint2;
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.
99 SkBitmap bitmap2;
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);
mtklein06d37712014-07-07 07:00:14 -0700105 paint2.setShader(shader)->unref();
tomhudsond9305112014-07-05 13:37:53 -0700106 REPORTER_ASSERT(r, shader->asABitmap(NULL, NULL, NULL) == SkShader::kDefault_BitmapType);
107
108 APPEND(record, SkRecords::DrawRect, paint2, rect);
109 REPORTER_ASSERT(r, SkRecordWillPlaybackBitmaps(record));
110}
111
112#undef APPEND
113