Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(1079)

Side by Side Diff: tests/ValueTest.cpp

Issue 1585813004: SkValue: SkXfermode (Closed) Base URL: https://skia.googlesource.com/skia.git@master
Patch Set: 2016-01-15 (Friday) 11:08:28 EST Created 4 years, 11 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
1 /* 1 /*
2 * Copyright 2016 Google Inc. 2 * Copyright 2016 Google Inc.
3 * 3 *
4 * Use of this source code is governed by a BSD-style license that can be 4 * Use of this source code is governed by a BSD-style license that can be
5 * found in the LICENSE file. 5 * found in the LICENSE file.
6 */ 6 */
7 7
8 #include "SkArithmeticMode.h"
9 #include "SkLerpXfermode.h"
10 #include "SkPixelXorXfermode.h"
11 #include "SkStream.h"
12 #include "SkValue.h"
13 #include "SkXfermode.h"
8 #include "Test.h" 14 #include "Test.h"
9 #include "SkValue.h" 15
16 static void print_value(bool verbose, const SkValue&);
17
18 static const SkValue::Type example_type = SkValue::Type(0x80000001);
19
20 enum { kExampleS32, kExampleF32, kExampleU32, kExampleObject};
21
22 #define EXAMPLES(FN) \
23 FN(kExampleS32, S32, s32, -123) \
24 FN(kExampleF32, F32, f32, 0.5f) \
25 FN(kExampleU32, U32, u32, 1234) \
26
27 static SkValue make_example(skiatest::Reporter* r, int level = 4) {
28 auto value = SkValue::Object(example_type);
29 REPORTER_ASSERT(r, value.set(kExampleU32, SkValue::FromU32(1000)));
30
31 #define FN(KEY, FNAME, MNAME, VALUE) \
32 REPORTER_ASSERT(r, value.set(KEY, SkValue::From##FNAME(VALUE)));
33 EXAMPLES(FN)
34 #undef FN
35 if (level > 0) {
36 value.set(kExampleObject, make_example(r, 0));
37 value.set(kExampleObject, make_example(r, level - 1)); // replace
38 }
39 return value;
40 }
41
42 DEF_TEST(Value_Test, r) {
43 SkValue val = make_example(r);
44 REPORTER_ASSERT(r, example_type == val.type());
45 REPORTER_ASSERT(r, val.set(4321, SkValue()));
46 SkValue valCopy = val;
47 REPORTER_ASSERT(r, example_type == valCopy.type());
48 REPORTER_ASSERT(r, !valCopy.set(12345, SkValue()));
49 REPORTER_ASSERT(r, !val.set(123456, SkValue()));
50 #define FN(KEY, FNAME, MNAME, VALUE) { \
51 const SkValue* v = val.get(KEY); \
52 REPORTER_ASSERT(r, v && VALUE == v->MNAME()); \
53 }
54 EXAMPLES(FN)
55 #undef FN
56 print_value(r->verbose(), val);
57 }
58
59 template <class T>
60 void test_value(skiatest::Reporter* r, T* object, SkValue::Type type) {
61 const SkValue val = object->asValue();
62 object->unref();
63 REPORTER_ASSERT(r, type == val.type());
64 print_value(r->verbose(), val);
65 }
66
67 DEF_TEST(Value_Xfermode, r) {
68 // just test that these code paths work until we hook them up to a DM sink.
69 test_value(r, SkXfermode::Create(SkXfermode::kDstOver_Mode),
70 SkValue::ProcCoeffXfermode);
71 test_value(r, SkArithmeticMode::Create(0.125f, 0.25f, 0.375f, 0.5f, true),
72 SkValue::ArithmeticXfermode);
73 test_value(r, SkLerpXfermode::Create(1.0f/3.0f), SkValue::LerpXfermode);
74 test_value(r, SkPixelXorXfermode::Create(0xFF00FF00),
75 SkValue::PixelXorXfermode);
76 }
77
78 ////////////////////////////////////////////////////////////////////////////////
79
80 // Conditional print. Always evaluate the arguments.
81 static void do_nothing(const char*, ...) {}
82 #define CPRINT(DO_PRINT) ((DO_PRINT) ? SkDebugf : do_nothing)
83
84 static void dump(bool verbose, int indent, const SkValue&);
85 static void dump_object(bool verbose, int indent,
86 const char* name, const SkValue& val) {
87 CPRINT(verbose)("[ \"%s\", {\n", name);
88 // todo: sort keys
89 val.foreach([=](SkValue::Key k, const SkValue& v){
90 for (int i = 0; i < indent; ++i) {
91 CPRINT(verbose)(" ");
92 }
93 CPRINT(verbose)(" \"key_%u\" : ", k);
94 dump(verbose, indent + 1, v);
95 CPRINT(verbose)("\n");
96 });
97 for (int i = 0; i < indent; ++i) {
98 CPRINT(verbose)(" ");
99 }
100 CPRINT(verbose)("} ],");
101 }
102
103 static void dump(bool verbose, int indent, const SkValue& val) {
104 switch (val.type()) {
105 case SkValue::Null:
106 CPRINT(verbose)("[ \"Null\", null ],");
107 return;
108 case SkValue::S32:
109 CPRINT(verbose)("[ \"S32\", %d ],", val.s32());
110 return;
111 case SkValue::U32:
112 CPRINT(verbose)("[ \"U32\", %u ],", val.u32());
113 return;
114 case SkValue::F32:
115 CPRINT(verbose)("[ \"F32\", %.9g ],", val.f32());
116 return;
117 #define FN(OBJECT) \
118 case SkValue::OBJECT: dump_object(verbose, indent, #OBJECT, val); re turn;
119 SK_VALUE_OBJECT_TYPES(FN)
120 #undef FN
121 default:
122 if (val.type() >= 0x80000000) {
123 SkString object = SkStringPrintf("Test_0x%X", val.type());
124 dump_object(verbose, indent, object.c_str(), val);
125 return;
126 }
127 SkASSERT(false);
128 }
129 }
130
131 static void print_value(bool verbose, const SkValue& val) {
132 CPRINT(verbose)("\n");
133 dump(verbose, 0, val);
134 CPRINT(verbose)("\n");
135 }
OLDNEW
« src/utils/debugger/SkDebugCanvas.cpp ('K') | « src/utils/debugger/SkDebugCanvas.cpp ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698