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

Side by Side Diff: src/core/SkNormalSource.cpp

Issue 2080993002: Added API for Bevel NormalSource. (Closed) Base URL: https://skia.googlesource.com/skia@dvonbeck-diffuse-api-change
Patch Set: Small init fix Created 4 years, 5 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 "SkError.h" 8 #include "SkError.h"
9 #include "SkErrorInternals.h" 9 #include "SkErrorInternals.h"
10 #include "SkLightingShader.h" 10 #include "SkLightingShader.h"
(...skipping 33 matching lines...) Expand 10 before | Expand all | Expand 10 after
44 bool computeNormTotalInverse(const SkShader::ContextRec& rec, SkMatrix* norm TotalInverse) const; 44 bool computeNormTotalInverse(const SkShader::ContextRec& rec, SkMatrix* norm TotalInverse) const;
45 45
46 private: 46 private:
47 class Provider : public SkNormalSource::Provider { 47 class Provider : public SkNormalSource::Provider {
48 public: 48 public:
49 Provider(const NormalMapSourceImpl& source, SkShader::Context* fMapConte xt); 49 Provider(const NormalMapSourceImpl& source, SkShader::Context* fMapConte xt);
50 50
51 virtual ~Provider() override; 51 virtual ~Provider() override;
52 52
53 void fillScanLine(int x, int y, SkPoint3 output[], int count) const over ride; 53 void fillScanLine(int x, int y, SkPoint3 output[], int count) const over ride;
54
54 private: 55 private:
55 const NormalMapSourceImpl& fSource; 56 const NormalMapSourceImpl& fSource;
56 SkShader::Context* fMapContext; 57 SkShader::Context* fMapContext;
57 58
58 typedef SkNormalSource::Provider INHERITED; 59 typedef SkNormalSource::Provider INHERITED;
59 }; 60 };
60 61
61 sk_sp<SkShader> fMapShader; 62 sk_sp<SkShader> fMapShader;
62 SkMatrix fInvCTM; // Inverse of the canvas total matrix, used for rot ating normals. 63 SkMatrix fInvCTM; // Inverse of the canvas total matrix, used for rot ating normals.
63 64
(...skipping 126 matching lines...) Expand 10 before | Expand all | Expand 10 after
190 return sk_make_sp<NormalMapFP>(std::move(mapFP), fInvCTM); 191 return sk_make_sp<NormalMapFP>(std::move(mapFP), fInvCTM);
191 } 192 }
192 193
193 #endif // SK_SUPPORT_GPU 194 #endif // SK_SUPPORT_GPU
194 195
195 //////////////////////////////////////////////////////////////////////////// 196 ////////////////////////////////////////////////////////////////////////////
196 197
197 NormalMapSourceImpl::Provider::Provider(const NormalMapSourceImpl& source, 198 NormalMapSourceImpl::Provider::Provider(const NormalMapSourceImpl& source,
198 SkShader::Context* mapContext) 199 SkShader::Context* mapContext)
199 : fSource(source) 200 : fSource(source)
200 , fMapContext(mapContext) { 201 , fMapContext(mapContext) {}
201 }
202 202
203 NormalMapSourceImpl::Provider::~Provider() { 203 NormalMapSourceImpl::Provider::~Provider() {
204 fMapContext->~Context(); 204 fMapContext->~Context();
205 } 205 }
206 206
207 SkNormalSource::Provider* NormalMapSourceImpl::asProvider( 207 SkNormalSource::Provider* NormalMapSourceImpl::asProvider(
208 const SkShader::ContextRec &rec, void *storage) const { 208 const SkShader::ContextRec &rec, void *storage) const {
209 SkMatrix normTotalInv; 209 SkMatrix normTotalInv;
210 if (!this->computeNormTotalInverse(rec, &normTotalInv)) { 210 if (!this->computeNormTotalInverse(rec, &normTotalInv)) {
211 return nullptr; 211 return nullptr;
(...skipping 97 matching lines...) Expand 10 before | Expand all | Expand 10 after
309 sk_sp<SkNormalSource> SkNormalSource::MakeFromNormalMap(sk_sp<SkShader> map, con st SkMatrix& ctm) { 309 sk_sp<SkNormalSource> SkNormalSource::MakeFromNormalMap(sk_sp<SkShader> map, con st SkMatrix& ctm) {
310 SkMatrix invCTM; 310 SkMatrix invCTM;
311 311
312 if (!ctm.invert(&invCTM) || !map) { 312 if (!ctm.invert(&invCTM) || !map) {
313 return nullptr; 313 return nullptr;
314 } 314 }
315 315
316 return sk_make_sp<NormalMapSourceImpl>(std::move(map), invCTM); 316 return sk_make_sp<NormalMapSourceImpl>(std::move(map), invCTM);
317 } 317 }
318 318
319 ///////////////////////////////////////////////////////////////////////////////
320
321 class SK_API NormalBevelSourceImpl : public SkNormalSource {
322 public:
323 NormalBevelSourceImpl(BevelType type, SkScalar width, SkScalar height)
324 : fType(type)
325 , fWidth(width)
326 , fHeight(height) {}
327
328 #if SK_SUPPORT_GPU
329 sk_sp<GrFragmentProcessor> asFragmentProcessor(GrContext*,
330 const SkMatrix& viewM,
331 const SkMatrix* localMatrix,
332 SkFilterQuality,
333 SkSourceGammaTreatment) const override;
334 #endif
335
336 SkNormalSource::Provider* asProvider(const SkShader::ContextRec& rec, void* storage) const
robertphillips 2016/07/11 19:13:21 orphan
dvonbeck 2016/07/13 14:23:37 Done.
337 override;
338 size_t providerSize(const SkShader::ContextRec& rec) const override;
339
340 SK_DECLARE_PUBLIC_FLATTENABLE_DESERIALIZATION_PROCS(NormalBevelSourceImpl)
341
342 protected:
343 void flatten(SkWriteBuffer& buf) const override;
344
345 private:
346 class Provider : public SkNormalSource::Provider {
347 public:
348 Provider(const NormalBevelSourceImpl& source);
349
350 virtual ~Provider();
351
352 void fillScanLine(int x, int y, SkPoint3 output[], int count) const over ride;
353
354 private:
355 const NormalBevelSourceImpl& fSource;
356
357 typedef SkNormalSource::Provider INHERITED;
358 };
359
360 SkNormalSource::BevelType fType;
361 SkScalar fWidth;
362 SkScalar fHeight;
363
robertphillips 2016/07/11 19:13:21 This is the base class. Does it need to be friende
dvonbeck 2016/07/13 14:23:37 No, it used to be when the structure was different
dvonbeck 2016/07/13 16:30:16 Nevermind, it is necessary for serialization to wo
364 friend class SkNormalSource;
365
366 typedef SkNormalSource INHERITED;
367 };
368
369 ////////////////////////////////////////////////////////////////////////////
370
371 #if SK_SUPPORT_GPU
372
robertphillips 2016/07/11 19:13:21 ??
dvonbeck 2016/07/13 14:23:38 Done.
373 #if 0 // Keeping here for reference in case code gets refactored into another fi le
374 #include "GrCoordTransform.h"
375 #include "GrInvariantOutput.h"
376 #include "GrTextureParams.h"
377 #include "glsl/GrGLSLFragmentProcessor.h"
378 #include "glsl/GrGLSLFragmentShaderBuilder.h"
379 #include "SkGr.h"
380 #endif
381
382 class NormalBevelFP : public GrFragmentProcessor {
383 public:
384 NormalBevelFP(SkNormalSource::BevelType type, SkScalar width, SkScalar heigh t)
385 : fType(type)
386 , fWidth(width)
387 , fHeight(height) {
388 this->initClassID<NormalBevelFP>();
389 }
390
391 class GLSLNormalBevelFP : public GrGLSLFragmentProcessor {
392 public:
393 GLSLNormalBevelFP() {}
394
395 void emitCode(EmitArgs& args) override {
396 GrGLSLFragmentBuilder* fragBuilder = args.fFragBuilder;
397
398 fragBuilder->codeAppendf("%s = vec4(0, 0, 1, 0);", args.fOutputColor );
399 }
400
401 static void GenKey(const GrProcessor& proc, const GrGLSLCaps&,
402 GrProcessorKeyBuilder* b) {
403 b->add32(0x0);
404 }
405
406 protected:
407 void onSetData(const GrGLSLProgramDataManager& pdman, const GrProcessor& proc) override {
408 const NormalBevelFP& normalBevelFP = proc.cast<NormalBevelFP>();
409
410 fType = normalBevelFP.fType;
411 fWidth = normalBevelFP.fWidth;
412 fHeight = normalBevelFP.fHeight;
413 }
414
415 private:
416 SkNormalSource::BevelType fType;
417 SkScalar fWidth;
418 SkScalar fHeight;
419 };
420
421 void onGetGLSLProcessorKey(const GrGLSLCaps& caps, GrProcessorKeyBuilder* b) const override {
422 GLSLNormalBevelFP::GenKey(*this, caps, b);
423 }
424
425 const char* name() const override { return "NormalBevelFP"; }
426
427 void onComputeInvariantOutput(GrInvariantOutput* inout) const override {
428 inout->setToUnknown(GrInvariantOutput::ReadInput::kWillNot_ReadInput);
429 }
430
431 private:
432 GrGLSLFragmentProcessor* onCreateGLSLInstance() const override { return new GLSLNormalBevelFP; }
433
434 bool onIsEqual(const GrFragmentProcessor& proc) const override {
435 const NormalBevelFP& normalBevelFP = proc.cast<NormalBevelFP>();
436 return fType == normalBevelFP.fType &&
437 fWidth == normalBevelFP.fWidth &&
438 fHeight == normalBevelFP.fHeight;
439 }
440
441 SkNormalSource::BevelType fType;
442 SkScalar fWidth;
443 SkScalar fHeight;
444 };
445
446 sk_sp<GrFragmentProcessor> NormalBevelSourceImpl::asFragmentProcessor(
447 GrContext *context,
448 const SkMatrix &viewM,
449 const SkMatrix *localMatrix ,
450 SkFilterQuality filterQuali ty,
451 SkSourceGammaTreatment gamm aTreatment) const {
452
453 return sk_make_sp<NormalBevelFP>(fType, fWidth, fHeight);
454 }
455
456 #endif // SK_SUPPORT_GPU
457
458 ////////////////////////////////////////////////////////////////////////////
459
460 NormalBevelSourceImpl::Provider::Provider(const NormalBevelSourceImpl& source)
461 : fSource(source) {}
462
463 NormalBevelSourceImpl::Provider::~Provider() {}
464
465 SkNormalSource::Provider* NormalBevelSourceImpl::asProvider(const SkShader::Cont extRec &rec,
466 void *storage) const {
467 return new (storage) Provider(*this);
468 }
469
470 size_t NormalBevelSourceImpl::providerSize(const SkShader::ContextRec&) const {
471 return sizeof(Provider);
472 }
473
474 void NormalBevelSourceImpl::Provider::fillScanLine(int x, int y, SkPoint3 output [], int count)
robertphillips 2016/07/11 19:13:21 orphan
dvonbeck 2016/07/13 14:23:37 Done.
475 const {
476 for (int i = 0; i < count; i++) {
477 output[i] = {0, 0, 1.0};
478 }
479 }
480
481 ////////////////////////////////////////////////////////////////////////////////
482
483 sk_sp<SkFlattenable> NormalBevelSourceImpl::CreateProc(SkReadBuffer& buf) {
484
485 auto type = static_cast<SkNormalSource::BevelType>(buf.readInt());
486 SkScalar width = buf.readScalar();
487 SkScalar height = buf.readScalar();
488
489 return sk_make_sp<NormalBevelSourceImpl>(type, width, height);
490 }
491
492 void NormalBevelSourceImpl::flatten(SkWriteBuffer& buf) const {
493 this->INHERITED::flatten(buf);
494
495 buf.writeInt(static_cast<int>(fType));
496 buf.writeScalar(fWidth);
497 buf.writeScalar(fHeight);
498 }
499
500 ////////////////////////////////////////////////////////////////////////////
501
502 sk_sp<SkNormalSource> SkNormalSource::MakeBevel(BevelType type, SkScalar width, SkScalar height) {
503 return sk_make_sp<NormalBevelSourceImpl>(type, width, height);
504 }
505
506 ////////////////////////////////////////////////////////////////////////////
507
319 //////////////////////////////////////////////////////////////////////////// 508 ////////////////////////////////////////////////////////////////////////////
320 509
321 SK_DEFINE_FLATTENABLE_REGISTRAR_GROUP_START(SkNormalSource) 510 SK_DEFINE_FLATTENABLE_REGISTRAR_GROUP_START(SkNormalSource)
322 SK_DEFINE_FLATTENABLE_REGISTRAR_ENTRY(NormalMapSourceImpl) 511 SK_DEFINE_FLATTENABLE_REGISTRAR_ENTRY(NormalMapSourceImpl)
512 SK_DEFINE_FLATTENABLE_REGISTRAR_ENTRY(NormalBevelSourceImpl)
323 SK_DEFINE_FLATTENABLE_REGISTRAR_GROUP_END 513 SK_DEFINE_FLATTENABLE_REGISTRAR_GROUP_END
324 514
325 //////////////////////////////////////////////////////////////////////////// 515 ////////////////////////////////////////////////////////////////////////////
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698