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

Side by Side Diff: include/core/SkLights.h

Issue 2206823003: Fix dtor bug in SkLights (Closed) Base URL: https://skia.googlesource.com/skia.git@master
Patch Set: update Created 4 years, 4 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
« no previous file with comments | « no previous file | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 1
2 /* 2 /*
3 * Copyright 2015 Google Inc. 3 * Copyright 2015 Google Inc.
4 * 4 *
5 * Use of this source code is governed by a BSD-style license that can be 5 * Use of this source code is governed by a BSD-style license that can be
6 * found in the LICENSE file. 6 * found in the LICENSE file.
7 */ 7 */
8 8
9 #ifndef SkLights_DEFINED 9 #ifndef SkLights_DEFINED
10 #define SkLights_DEFINED 10 #define SkLights_DEFINED
11 11
12 #include "SkPoint3.h" 12 #include "SkPoint3.h"
13 #include "SkRefCnt.h" 13 #include "SkRefCnt.h"
14 #include "../private/SkTDArray.h" 14 #include "../private/SkTArray.h"
15 #include "SkImage.h" 15 #include "SkImage.h"
16 16
17 class SK_API SkLights : public SkRefCnt { 17 class SK_API SkLights : public SkRefCnt {
18 public: 18 public:
19 class Light { 19 class Light {
20 public: 20 public:
21 enum LightType { 21 enum LightType {
22 kAmbient_LightType, // only 'fColor' is used 22 kAmbient_LightType, // only 'fColor' is used
23 kDirectional_LightType 23 kDirectional_LightType
24 }; 24 };
25 25
26 Light(const Light& other)
27 : fType(other.fType)
28 , fColor(other.fColor)
29 , fDirection(other.fDirection)
30 , fShadowMap(other.fShadowMap) {
31 }
32
33 Light(Light&& other)
34 : fType(other.fType)
35 , fColor(other.fColor)
36 , fDirection(other.fDirection)
37 , fShadowMap(std::move(other.fShadowMap)) {
38 }
39
26 Light(const SkColor3f& color) 40 Light(const SkColor3f& color)
27 : fType(kAmbient_LightType) 41 : fType(kAmbient_LightType)
28 , fColor(color) { 42 , fColor(color) {
29 fDirection.set(0.0f, 0.0f, 1.0f); 43 fDirection.set(0.0f, 0.0f, 1.0f);
30 fShadowMap.reset(nullptr);
31 } 44 }
32 45
33 Light(const SkColor3f& color, const SkVector3& dir) 46 Light(const SkColor3f& color, const SkVector3& dir)
34 : fType(kDirectional_LightType) 47 : fType(kDirectional_LightType)
35 , fColor(color) 48 , fColor(color)
36 , fDirection(dir) { 49 , fDirection(dir) {
37 if (!fDirection.normalize()) { 50 if (!fDirection.normalize()) {
38 fDirection.set(0.0f, 0.0f, 1.0f); 51 fDirection.set(0.0f, 0.0f, 1.0f);
39 } 52 }
40 fShadowMap.reset(nullptr);
41 } 53 }
42 54
43 LightType type() const { return fType; } 55 LightType type() const { return fType; }
44 const SkColor3f& color() const { return fColor; } 56 const SkColor3f& color() const { return fColor; }
45 const SkVector3& dir() const { 57 const SkVector3& dir() const {
46 SkASSERT(kAmbient_LightType != fType); 58 SkASSERT(kAmbient_LightType != fType);
47 return fDirection; 59 return fDirection;
48 } 60 }
49 61
50 void setShadowMap(sk_sp<SkImage> shadowMap) { 62 void setShadowMap(sk_sp<SkImage> shadowMap) {
51 fShadowMap = std::move(shadowMap); 63 fShadowMap = std::move(shadowMap);
52 } 64 }
53 65
54 sk_sp<SkImage> getShadowMap() const { 66 SkImage* getShadowMap() const {
55 return fShadowMap; 67 return fShadowMap.get();
56 } 68 }
57 69
58 Light& operator= (const Light& b) { 70 Light& operator= (const Light& b) {
59 if (this == &b) 71 if (this == &b) {
60 return *this; 72 return *this;
61
62 this->fColor = b.fColor;
63 this->fType = b.fType;
64 this->fDirection = b.fDirection;
65
66 if (b.fShadowMap) {
67 this->fShadowMap = b.fShadowMap;
68 } 73 }
69 74
75 fColor = b.fColor;
76 fType = b.fType;
77 fDirection = b.fDirection;
78 fShadowMap = b.fShadowMap;
70 return *this; 79 return *this;
71 } 80 }
72 81
73 private: 82 private:
74 LightType fType; 83 LightType fType;
75 SkColor3f fColor; // linear (unpremul) color. Range is 0..1 in each channel. 84 SkColor3f fColor; // linear (unpremul) color. Range is 0..1 in each channel.
76 SkVector3 fDirection; // direction towards the light (+Z is out of the screen). 85 SkVector3 fDirection; // direction towards the light (+Z is out of the screen).
77 // If degenerate, it will be replaced with (0, 0, 1). 86 // If degenerate, it will be replaced with (0, 0, 1).
78 sk_sp<SkImage> fShadowMap; 87 sk_sp<SkImage> fShadowMap;
79 }; 88 };
80 89
81 class Builder { 90 class Builder {
82 public: 91 public:
83 Builder() : fLights(new SkLights) { } 92 Builder() : fLights(new SkLights) { }
84 93
85 void add(const Light& light) { 94 void add(const Light& light) {
86 if (fLights) { 95 if (fLights) {
87 (void) fLights->fLights.append(1, &light); 96 fLights->fLights.push_back(light);
97 }
98 }
99
100 void add(Light&& light) {
101 if (fLights) {
102 fLights->fLights.push_back(std::move(light));
88 } 103 }
89 } 104 }
90 105
91 sk_sp<SkLights> finish() { 106 sk_sp<SkLights> finish() {
92 return fLights; 107 return std::move(fLights);
93 } 108 }
94 109
95 private: 110 private:
96 sk_sp<SkLights> fLights; 111 sk_sp<SkLights> fLights;
97 }; 112 };
98 113
99 int numLights() const { 114 int numLights() const {
100 return fLights.count(); 115 return fLights.count();
101 } 116 }
102 117
103 const Light& light(int index) const { 118 const Light& light(int index) const {
104 return fLights[index]; 119 return fLights[index];
105 } 120 }
106 121
107 Light& light(int index) { 122 Light& light(int index) {
108 return fLights[index]; 123 return fLights[index];
109 } 124 }
110 125
111 private: 126 private:
112 SkLights() {} 127 SkLights() {}
113 128
114 SkTDArray<Light> fLights; 129 SkTArray<Light> fLights;
130
131 typedef SkRefCnt INHERITED;
115 }; 132 };
116 133
117 #endif 134 #endif
OLDNEW
« no previous file with comments | « no previous file | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698