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 "SkBitmapProcShader.h" | 8 #include "SkBitmapProcShader.h" |
9 #include "SkBitmapProcState.h" | |
9 #include "SkError.h" | 10 #include "SkError.h" |
10 #include "SkErrorInternals.h" | 11 #include "SkErrorInternals.h" |
11 #include "SkLightingShader.h" | 12 #include "SkLightingShader.h" |
12 #include "SkReadBuffer.h" | 13 #include "SkReadBuffer.h" |
13 #include "SkWriteBuffer.h" | 14 #include "SkWriteBuffer.h" |
14 | 15 |
15 // Genretating vtable | 16 // Genretating vtable |
16 SkLightingShader::NormalSource::~NormalSource() {} | 17 SkLightingShader::NormalSource::~NormalSource() {} |
17 | 18 |
18 /////////////////////////////////////////////////////////////////////////////// | 19 /////////////////////////////////////////////////////////////////////////////// |
(...skipping 15 matching lines...) Expand all Loading... | |
34 } | 35 } |
35 | 36 |
36 #if SK_SUPPORT_GPU | 37 #if SK_SUPPORT_GPU |
37 sk_sp<GrFragmentProcessor> asFragmentProcessor(GrContext*, | 38 sk_sp<GrFragmentProcessor> asFragmentProcessor(GrContext*, |
38 const SkMatrix& viewM, | 39 const SkMatrix& viewM, |
39 const SkMatrix* localMatrix, | 40 const SkMatrix* localMatrix, |
40 SkFilterQuality, | 41 SkFilterQuality, |
41 SkSourceGammaTreatment) const override; | 42 SkSourceGammaTreatment) const override; |
42 #endif | 43 #endif |
43 | 44 |
45 SkLightingShader::NormalSource::Provider* asProvider(const SkShader::Context Rec& rec, | |
46 void* storage) const ov erride; | |
47 | |
48 size_t providerSize() const override; | |
49 | |
44 SK_DECLARE_PUBLIC_FLATTENABLE_DESERIALIZATION_PROCS(NormalMapSourceImpl) | 50 SK_DECLARE_PUBLIC_FLATTENABLE_DESERIALIZATION_PROCS(NormalMapSourceImpl) |
45 | 51 |
46 protected: | 52 protected: |
47 void flatten(SkWriteBuffer& buf) const override; | 53 void flatten(SkWriteBuffer& buf) const override; |
48 | 54 |
49 private: | 55 private: |
56 | |
57 class Provider : public SkLightingShader::NormalSource::Provider { | |
58 public: | |
59 Provider(const NormalMapSourceImpl& source, SkBitmapProcState* state); | |
60 | |
61 virtual ~Provider(); | |
62 | |
63 void fillScanLine(int x, int y, SkPoint3 output[], int count) const over ride; | |
64 | |
65 int maxCountForFillScanLine(int count) const override; | |
66 private: | |
67 const NormalMapSourceImpl& fSource; | |
egdaniel
2016/06/16 13:48:10
Generally we store pointers here if you just need
dvonbeck
2016/06/16 22:05:40
I did this because it's what LightingShader contex
| |
68 SkBitmapProcState* fState; | |
69 | |
70 typedef SkLightingShader::NormalSource::Provider INHERITED; | |
71 }; | |
72 | |
73 bool computeNormTotalInverse(const SkShader::ContextRec& rec, SkMatrix* norm TotalInverse) const; | |
74 | |
50 SkBitmap fNormalMap; | 75 SkBitmap fNormalMap; |
51 SkMatrix fNormLocalMatrix; | 76 SkMatrix fNormLocalMatrix; |
52 SkVector fInvNormRotation; | 77 SkVector fInvNormRotation; |
53 | 78 |
54 friend class SkLightingShader::NormalMapSource; | 79 friend class SkLightingShader::NormalMapSource; |
55 | 80 |
56 typedef SkLightingShader::NormalSource INHERITED; | 81 typedef SkLightingShader::NormalSource INHERITED; |
57 }; | 82 }; |
58 | 83 |
59 //////////////////////////////////////////////////////////////////////////// | 84 //////////////////////////////////////////////////////////////////////////// |
(...skipping 161 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
221 return nullptr; | 246 return nullptr; |
222 } | 247 } |
223 | 248 |
224 return sk_make_sp<NormalMapFP>(normalTexture, normM, normParams, fInvNormRot ation); | 249 return sk_make_sp<NormalMapFP>(normalTexture, normM, normParams, fInvNormRot ation); |
225 } | 250 } |
226 | 251 |
227 #endif // SK_SUPPORT_GPU | 252 #endif // SK_SUPPORT_GPU |
228 | 253 |
229 //////////////////////////////////////////////////////////////////////////// | 254 //////////////////////////////////////////////////////////////////////////// |
230 | 255 |
256 NormalMapSourceImpl::Provider::Provider(const NormalMapSourceImpl& source, SkBit mapProcState* state) | |
257 : fSource(source) | |
258 , fState(state) { | |
259 SkASSERT(fState->fPixmap.addr()); | |
260 } | |
261 | |
262 NormalMapSourceImpl::Provider::~Provider() { | |
263 fState->~SkBitmapProcState(); | |
264 } | |
265 | |
266 SkLightingShader::NormalSource::Provider* NormalMapSourceImpl::asProvider( | |
267 const SkShader::ContextRec &rec, void *storage) const { | |
268 SkMatrix normTotalInv; | |
269 if (!this->computeNormTotalInverse(rec, &normTotalInv)) { | |
270 return nullptr; | |
271 } | |
272 | |
273 void* stateStorage = (char*)storage + sizeof(Provider); | |
274 SkBitmapProcState* state = new (stateStorage) SkBitmapProcState(fNormalMap, | |
275 SkShader::kClamp_TileMode, SkShader::kClamp_TileMode, SkMipMap::Dedu ceTreatment(rec)); | |
276 SkASSERT(state); | |
277 if (!state->setup(normTotalInv, *rec.fPaint)) { | |
278 state->~SkBitmapProcState(); | |
279 return nullptr; | |
280 } | |
281 | |
282 return new (storage) Provider(*this, state); | |
283 } | |
284 | |
285 size_t NormalMapSourceImpl::providerSize() const { | |
286 return sizeof(Provider) + sizeof(SkBitmapProcState); | |
287 } | |
288 | |
289 // TODO: these defines are redundant with the ones in SkLightingShader.cpp, will consolidate in a | |
290 // future CL when the source of diffuse colores is factored out | |
291 #define TMP_COUNT 16 | |
292 #define BUFFER_MAX (TMP_COUNT * sizeof(uint32_t)) | |
293 void NormalMapSourceImpl::Provider::fillScanLine(int x, int y, SkPoint3 output[] , | |
294 int count) const { | |
295 SkBitmapProcState::MatrixProc normalMProc = fState->getMatrixProc(); | |
296 SkBitmapProcState::SampleProc32 normalSProc = fState->getSampleProc32(); | |
297 | |
298 uint32_t tmpNormal[BUFFER_MAX]; | |
299 SkPMColor tmpNormal2[2*BUFFER_MAX]; | |
300 normalMProc(*fState, tmpNormal, count, x, y); | |
301 normalSProc(*fState, tmpNormal, count, tmpNormal2); | |
302 | |
303 for (int i = 0; i < count; i++) { | |
304 SkASSERT(0xFF == SkColorGetA(tmpNormal2[i])); // opaque -> unpremul | |
305 | |
306 SkPoint3 tempNorm; | |
307 | |
308 tempNorm.set(SkIntToScalar(SkGetPackedR32(tmpNormal2[i])) - 127.0f, | |
309 SkIntToScalar(SkGetPackedG32(tmpNormal2[i])) - 127.0f, | |
310 SkIntToScalar(SkGetPackedB32(tmpNormal2[i])) - 127.0f); | |
311 tempNorm.normalize(); | |
312 | |
313 | |
314 output[i].fX = fSource.fInvNormRotation.fX * tempNorm.fX + | |
315 fSource.fInvNormRotation.fY * tempNorm.fY; | |
316 output[i].fY = -fSource.fInvNormRotation.fY * tempNorm.fX + | |
317 fSource.fInvNormRotation.fX * tempNorm.fY; | |
318 output[i].fZ = tempNorm.fZ; | |
319 } | |
320 } | |
321 | |
322 int NormalMapSourceImpl::Provider::maxCountForFillScanLine(int count) const { | |
323 return fState->maxCountForBufferSize(sizeof(uint32_t /*tmpNormal[0]*/) * cou nt); | |
324 } | |
325 | |
326 bool NormalMapSourceImpl::computeNormTotalInverse(const SkShader::ContextRec& re c, | |
327 SkMatrix* normTotalInverse) co nst { | |
328 SkMatrix total; | |
329 total.setConcat(*rec.fMatrix, fNormLocalMatrix); | |
330 | |
331 const SkMatrix* m = &total; | |
332 if (rec.fLocalMatrix) { | |
333 total.setConcat(*m, *rec.fLocalMatrix); | |
334 m = &total; | |
335 } | |
336 return m->invert(normTotalInverse); | |
337 } | |
338 | |
339 //////////////////////////////////////////////////////////////////////////////// | |
340 | |
231 sk_sp<SkFlattenable> NormalMapSourceImpl::CreateProc(SkReadBuffer& buf) { | 341 sk_sp<SkFlattenable> NormalMapSourceImpl::CreateProc(SkReadBuffer& buf) { |
232 | 342 |
233 SkMatrix normLocalM; | 343 SkMatrix normLocalM; |
234 bool hasNormLocalM = buf.readBool(); | 344 bool hasNormLocalM = buf.readBool(); |
235 if (hasNormLocalM) { | 345 if (hasNormLocalM) { |
236 buf.readMatrix(&normLocalM); | 346 buf.readMatrix(&normLocalM); |
237 } else { | 347 } else { |
238 normLocalM.reset(); | 348 normLocalM.reset(); |
239 } | 349 } |
240 | 350 |
(...skipping 40 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
281 return sk_make_sp<NormalMapSourceImpl>(normal, invNormRotation, normLocalM); | 391 return sk_make_sp<NormalMapSourceImpl>(normal, invNormRotation, normLocalM); |
282 } | 392 } |
283 | 393 |
284 //////////////////////////////////////////////////////////////////////////// | 394 //////////////////////////////////////////////////////////////////////////// |
285 | 395 |
286 SK_DEFINE_FLATTENABLE_REGISTRAR_GROUP_START(SkLightingShader::NormalMapSource) | 396 SK_DEFINE_FLATTENABLE_REGISTRAR_GROUP_START(SkLightingShader::NormalMapSource) |
287 SK_DEFINE_FLATTENABLE_REGISTRAR_ENTRY(NormalMapSourceImpl) | 397 SK_DEFINE_FLATTENABLE_REGISTRAR_ENTRY(NormalMapSourceImpl) |
288 SK_DEFINE_FLATTENABLE_REGISTRAR_GROUP_END | 398 SK_DEFINE_FLATTENABLE_REGISTRAR_GROUP_END |
289 | 399 |
290 //////////////////////////////////////////////////////////////////////////// | 400 //////////////////////////////////////////////////////////////////////////// |
OLD | NEW |