| OLD | NEW |
| 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 "SampleCode.h" | 8 #include "SampleCode.h" |
| 9 #include "SkPictureRecorder.h" | 9 #include "SkPictureRecorder.h" |
| 10 #include "SkShadowPaintFilterCanvas.h" | 10 #include "SkShadowPaintFilterCanvas.h" |
| 11 #include "SkShadowShader.h" | 11 #include "SkShadowShader.h" |
| 12 #include "SkSurface.h" | 12 #include "SkSurface.h" |
| 13 | 13 |
| 14 #ifdef SK_EXPERIMENTAL_SHADOWING | 14 #ifdef SK_EXPERIMENTAL_SHADOWING |
| 15 | 15 |
| 16 class ShadowingView : public SampleView { | 16 class ShadowingView : public SampleView { |
| 17 public: | 17 public: |
| 18 ShadowingView() { | 18 ShadowingView() |
| 19 : fSceneChanged(true) |
| 20 , fLightsChanged(true) |
| 21 , fMoveLight(false) |
| 22 , fClearShadowMaps(false) |
| 23 , fSelectedRectID(-1) |
| 24 , fLightDepth(300.0f) { |
| 25 this->setBGColor(0xFFCCCCCC); |
| 19 | 26 |
| 20 this->setBGColor(0xFFCCCCCC); | 27 this->updateLights(200, 200); |
| 21 SkLights::Builder builder; | |
| 22 builder.add(SkLights::Light::MakeDirectional(SkColor3f::Make(0.2f, 0.3f,
0.4f), | |
| 23 SkVector3::Make(0.2f, 0.05f
, 1.0f))); | |
| 24 builder.add(SkLights::Light::MakeDirectional(SkColor3f::Make(0.4f, 0.3f,
0.2f), | |
| 25 SkVector3::Make(0.05f, 0.2f
, 1.0f))); | |
| 26 builder.add(SkLights::Light::MakeAmbient(SkColor3f::Make(0.4f, 0.4f, 0.4
f))); | |
| 27 fLights = builder.finish(); | |
| 28 | 28 |
| 29 fTestRects[0].fColor = 0xFFEE8888; | 29 fTestRects[0].fColor = 0xFFEE8888; |
| 30 fTestRects[0].fDepth = 80; | 30 fTestRects[0].fDepth = 80; |
| 31 fTestRects[0].fGeometry = SkRect::MakeLTRB(200,150,350,300); | 31 fTestRects[0].fGeometry = SkRect::MakeLTRB(200,150,350,300); |
| 32 | 32 |
| 33 fTestRects[1].fColor = 0xFF88EE88; | 33 fTestRects[1].fColor = 0xFF88EE88; |
| 34 fTestRects[1].fDepth = 160; | 34 fTestRects[1].fDepth = 160; |
| 35 fTestRects[1].fGeometry = SkRect::MakeLTRB(150,200,300,350); | 35 fTestRects[1].fGeometry = SkRect::MakeLTRB(150,200,300,350); |
| 36 | 36 |
| 37 fTestRects[2].fColor = 0xFF8888EE; | 37 fTestRects[2].fColor = 0xFF8888EE; |
| 38 fTestRects[2].fDepth = 240; | 38 fTestRects[2].fDepth = 240; |
| 39 fTestRects[2].fGeometry = SkRect::MakeLTRB(100,100,250,250); | 39 fTestRects[2].fGeometry = SkRect::MakeLTRB(100,100,250,250); |
| 40 | |
| 41 fSceneChanged = true; | |
| 42 fLightsChanged = true; | |
| 43 | |
| 44 fSelectedRect = -1; | |
| 45 fMoveLight = false; | |
| 46 | |
| 47 fClearShadowMaps = false; | |
| 48 } | 40 } |
| 49 | 41 |
| 50 protected: | 42 protected: |
| 51 bool onQuery(SkEvent *evt) override { | 43 bool onQuery(SkEvent *evt) override { |
| 52 if (SampleCode::TitleQ(*evt)) { | 44 if (SampleCode::TitleQ(*evt)) { |
| 53 SampleCode::TitleR(evt, "shadowing"); | 45 SampleCode::TitleR(evt, "shadowing"); |
| 54 return true; | 46 return true; |
| 55 } | 47 } |
| 56 | 48 |
| 57 SkUnichar uni; | 49 SkUnichar uni; |
| 58 if (SampleCode::CharQ(*evt, &uni)) { | 50 if (SampleCode::CharQ(*evt, &uni)) { |
| 59 switch (uni) { | 51 switch (uni) { |
| 60 case 'L': | 52 case 'L': |
| 61 fMoveLight = !fMoveLight; | 53 fMoveLight = !fMoveLight; |
| 62 break; | 54 break; |
| 63 case 'd': | 55 case 'd': |
| 64 // Raster generated shadow maps have their origin in the UL
corner | 56 // Raster generated shadow maps have their origin in the UL
corner |
| 65 // GPU shadow maps can have an arbitrary origin. | 57 // GPU shadow maps can have an arbitrary origin. |
| 66 // We override the 'd' keypress so that when the device is c
ycled, | 58 // We override the 'd' keypress so that when the device is c
ycled, |
| 67 // the shadow maps will be re-generated according to the new
backend. | 59 // the shadow maps will be re-generated according to the new
backend. |
| 68 fClearShadowMaps = true; | 60 fClearShadowMaps = true; |
| 69 break; | 61 break; |
| 62 case 'q': |
| 63 fLightDepth += 5.0f; |
| 64 fMoveLight = true; |
| 65 break; |
| 66 case 'w': |
| 67 fLightDepth -= 5.0f; |
| 68 fMoveLight = true; |
| 69 break; |
| 70 default: | 70 default: |
| 71 break; | 71 break; |
| 72 } | 72 } |
| 73 } | 73 } |
| 74 return this->INHERITED::onQuery(evt); | 74 return this->INHERITED::onQuery(evt); |
| 75 } | 75 } |
| 76 | 76 |
| 77 sk_sp<SkPicture> makeTestPicture(int width, int height) { | 77 sk_sp<SkPicture> makeTestPicture(int width, int height) { |
| 78 SkPictureRecorder recorder; | 78 SkPictureRecorder recorder; |
| 79 | 79 |
| (...skipping 30 matching lines...) Expand all Loading... |
| 110 } | 110 } |
| 111 | 111 |
| 112 if (fSceneChanged || fLightsChanged || fClearShadowMaps) { | 112 if (fSceneChanged || fLightsChanged || fClearShadowMaps) { |
| 113 for (int i = 0; i < fLights->numLights(); i++) { | 113 for (int i = 0; i < fLights->numLights(); i++) { |
| 114 fLights->light(i).setShadowMap(nullptr); | 114 fLights->light(i).setShadowMap(nullptr); |
| 115 } | 115 } |
| 116 fSceneChanged = false; | 116 fSceneChanged = false; |
| 117 fLightsChanged = false; | 117 fLightsChanged = false; |
| 118 fClearShadowMaps = false; | 118 fClearShadowMaps = false; |
| 119 | 119 |
| 120 canvas->setLights(fLights); | |
| 121 canvas->drawShadowedPicture(fPicture, nullptr, nullptr); | |
| 122 } | 120 } |
| 121 canvas->setLights(fLights); |
| 122 canvas->drawShadowedPicture(fPicture, nullptr, nullptr); |
| 123 } | 123 } |
| 124 | 124 |
| 125 SkView::Click* onFindClickHandler(SkScalar x, SkScalar y, unsigned modi) ove
rride { | 125 SkView::Click* onFindClickHandler(SkScalar x, SkScalar y, unsigned modi) ove
rride { |
| 126 return new SkView::Click(this); | 126 return new SkView::Click(this); |
| 127 } | 127 } |
| 128 | 128 |
| 129 void updateLights(int x, int y) { |
| 130 SkLights::Builder builder; |
| 131 builder.add(SkLights::Light::MakePoint(SkColor3f::Make(0.3f, 0.5f, 0.7f)
, |
| 132 SkVector3::Make(x - 50, |
| 133 350 - y, |
| 134 fLightDepth), |
| 135 1024)); |
| 136 |
| 137 builder.add(SkLights::Light::MakePoint(SkColor3f::Make(0.7f, 0.5f, 0.3f)
, |
| 138 SkVector3::Make(x + 50, |
| 139 450 - y, |
| 140 fLightDepth), |
| 141 1024)); |
| 142 fLights = builder.finish(); |
| 143 } |
| 144 |
| 129 bool onClick(Click *click) override { | 145 bool onClick(Click *click) override { |
| 130 SkScalar x = click->fCurr.fX; | 146 SkScalar x = click->fCurr.fX; |
| 131 SkScalar y = click->fCurr.fY; | 147 SkScalar y = click->fCurr.fY; |
| 132 | 148 |
| 133 SkScalar dx = x - click->fPrev.fX; | 149 SkScalar dx = x - click->fPrev.fX; |
| 134 SkScalar dy = y - click->fPrev.fY; | 150 SkScalar dy = y - click->fPrev.fY; |
| 135 | 151 |
| 136 if (fMoveLight) { | 152 if (fMoveLight) { |
| 137 if (dx != 0 || dy != 0) { | 153 if (dx != 0 || dy != 0) { |
| 138 float recipX = 1.0f / kWidth; | 154 this->updateLights(x, y); |
| 139 float recipY = 1.0f / kHeight; | |
| 140 | |
| 141 SkLights::Builder builder; | |
| 142 builder.add(SkLights::Light::MakeDirectional( | |
| 143 SkColor3f::Make(0.2f, 0.3f, 0.4f), | |
| 144 SkVector3::Make(0.2f + (200.0f - x) * recipX, | |
| 145 0.05f + (200.0f - y) * recipY, | |
| 146 1.0f))); | |
| 147 builder.add(SkLights::Light::MakeDirectional( | |
| 148 SkColor3f::Make(0.4f, 0.3f, 0.2f), | |
| 149 SkVector3::Make(0.05f + (200.0f - x) * recipX, | |
| 150 0.2f + (200.0f - y) * recipY, | |
| 151 1.0f))); | |
| 152 builder.add(SkLights::Light::MakeAmbient( | |
| 153 SkColor3f::Make(0.4f, 0.4f, 0.4f))); | |
| 154 fLights = builder.finish(); | |
| 155 | |
| 156 fLightsChanged = true; | 155 fLightsChanged = true; |
| 157 this->inval(nullptr); | 156 this->inval(nullptr); |
| 158 } | 157 } |
| 159 return true; | 158 return true; |
| 160 } | 159 } |
| 161 | 160 |
| 162 if (click->fState == Click::State::kUp_State) { | 161 if (click->fState == Click::State::kUp_State) { |
| 163 fSelectedRect = -1; | 162 fSelectedRectID = -1; |
| 164 return true; | 163 return true; |
| 165 } | 164 } |
| 166 | 165 |
| 167 if (fSelectedRect > -1) { | 166 if (fSelectedRectID > -1) { |
| 168 fTestRects[fSelectedRect].fGeometry.offset(dx, dy); | 167 fTestRects[fSelectedRectID].fGeometry.offset(dx, dy); |
| 169 | 168 |
| 170 fSceneChanged = true; | 169 fSceneChanged = true; |
| 171 this->inval(nullptr); | 170 this->inval(nullptr); |
| 172 return true; | 171 return true; |
| 173 } | 172 } |
| 174 | 173 |
| 175 // assume last elements are highest | 174 // assume last elements are highest |
| 176 for (int i = kNumTestRects - 1; i >= 0; i--) { | 175 for (int i = kNumTestRects - 1; i >= 0; i--) { |
| 177 if (fTestRects[i].fGeometry.contains(SkRect::MakeXYWH(x, y, 1, 1)))
{ | 176 if (fTestRects[i].fGeometry.contains(SkRect::MakeXYWH(x, y, 1, 1)))
{ |
| 178 fSelectedRect = i; | 177 fSelectedRectID = i; |
| 179 fTestRects[i].fGeometry.offset(dx, dy); | 178 fTestRects[i].fGeometry.offset(dx, dy); |
| 180 | 179 |
| 181 fSceneChanged = true; | 180 fSceneChanged = true; |
| 182 this->inval(nullptr); | 181 this->inval(nullptr); |
| 183 break; | 182 break; |
| 184 } | 183 } |
| 185 } | 184 } |
| 186 | 185 |
| 187 return true; | 186 return true; |
| 188 } | 187 } |
| 189 | 188 |
| 190 private: | 189 private: |
| 191 static constexpr int kNumTestRects = 3; | 190 static constexpr int kNumTestRects = 3; |
| 192 | 191 |
| 193 static const int kWidth = 400; | 192 static const int kWidth = 400; |
| 194 static const int kHeight = 400; | 193 static const int kHeight = 400; |
| 194 |
| 195 bool fSceneChanged; |
| 196 bool fLightsChanged; |
| 197 bool fMoveLight; |
| 195 bool fClearShadowMaps; | 198 bool fClearShadowMaps; |
| 196 | 199 |
| 197 struct { | 200 struct { |
| 198 SkRect fGeometry; | 201 SkRect fGeometry; |
| 199 int fDepth; | 202 int fDepth; |
| 200 SkColor fColor; | 203 SkColor fColor; |
| 201 } fTestRects[kNumTestRects]; | 204 } fTestRects[kNumTestRects]; |
| 202 | 205 |
| 203 int fSelectedRect; | 206 int fSelectedRectID; |
| 204 bool fMoveLight; | 207 SkScalar fLightDepth; |
| 205 | 208 |
| 206 sk_sp<SkPicture> fPicture; | 209 sk_sp<SkPicture> fPicture; |
| 207 | 210 |
| 208 bool fSceneChanged; | |
| 209 bool fLightsChanged; | |
| 210 | |
| 211 sk_sp<SkLights> fLights; | 211 sk_sp<SkLights> fLights; |
| 212 | 212 |
| 213 typedef SampleView INHERITED; | 213 typedef SampleView INHERITED; |
| 214 }; | 214 }; |
| 215 | 215 |
| 216 ////////////////////////////////////////////////////////////////////////////// | 216 ////////////////////////////////////////////////////////////////////////////// |
| 217 static SkView* MyFactory() { return new ShadowingView; } | 217 static SkView* MyFactory() { return new ShadowingView; } |
| 218 static SkViewRegister reg(MyFactory); | 218 static SkViewRegister reg(MyFactory); |
| 219 | 219 |
| 220 #endif | 220 #endif |
| OLD | NEW |