1 /*
2  * Copyright 2016 Google Inc.
3  *
4  * Use of this source code is governed by a BSD-style license that can be
5  * found in the LICENSE file.
6  */
7 
8 #include "SkNormalFlatSource.h"
9 
10 #include "SkArenaAlloc.h"
11 #include "SkNormalSource.h"
12 #include "SkPoint3.h"
13 #include "SkReadBuffer.h"
14 #include "SkWriteBuffer.h"
15 
16 #if SK_SUPPORT_GPU
17 #include "glsl/GrGLSLFragmentProcessor.h"
18 #include "glsl/GrGLSLFragmentShaderBuilder.h"
19 
20 class NormalFlatFP : public GrFragmentProcessor {
21 public:
Make()22     static std::unique_ptr<GrFragmentProcessor> Make() {
23         return std::unique_ptr<GrFragmentProcessor>(new NormalFlatFP());
24     }
25 
name() const26     const char* name() const override { return "NormalFlatFP"; }
27 
clone() const28     std::unique_ptr<GrFragmentProcessor> clone() const override { return Make(); }
29 
30 private:
31     class GLSLNormalFlatFP : public GrGLSLFragmentProcessor {
32     public:
GLSLNormalFlatFP()33         GLSLNormalFlatFP() {}
34 
emitCode(EmitArgs & args)35         void emitCode(EmitArgs& args) override {
36             GrGLSLFPFragmentBuilder* fragBuilder = args.fFragBuilder;
37 
38             fragBuilder->codeAppendf("%s = half4(0, 0, 1, 0);", args.fOutputColor);
39         }
40 
41     private:
onSetData(const GrGLSLProgramDataManager &,const GrFragmentProcessor &)42         void onSetData(const GrGLSLProgramDataManager&, const GrFragmentProcessor&) override {}
43     };
44 
NormalFlatFP()45     NormalFlatFP()
46             : INHERITED(kFlatNormalsFP_ClassID, kConstantOutputForConstantInput_OptimizationFlag) {
47     }
48 
onGetGLSLProcessorKey(const GrShaderCaps & caps,GrProcessorKeyBuilder * b) const49     void onGetGLSLProcessorKey(const GrShaderCaps& caps, GrProcessorKeyBuilder* b) const override {}
50 
constantOutputForConstantInput(const SkPMColor4f &) const51     SkPMColor4f constantOutputForConstantInput(const SkPMColor4f&) const override {
52         return { 0, 0, 1, 0 };
53     }
onCreateGLSLInstance() const54     GrGLSLFragmentProcessor* onCreateGLSLInstance() const override { return new GLSLNormalFlatFP; }
55 
onIsEqual(const GrFragmentProcessor &) const56     bool onIsEqual(const GrFragmentProcessor&) const override { return true; }
57 
58     typedef GrFragmentProcessor INHERITED;
59 };
60 
asFragmentProcessor(const GrFPArgs &) const61 std::unique_ptr<GrFragmentProcessor> SkNormalFlatSourceImpl::asFragmentProcessor(
62                                                                             const GrFPArgs&) const {
63     return NormalFlatFP::Make();
64 }
65 
66 #endif // SK_SUPPORT_GPU
67 
68 ////////////////////////////////////////////////////////////////////////////
69 
Provider()70 SkNormalFlatSourceImpl::Provider::Provider() {}
71 
~Provider()72 SkNormalFlatSourceImpl::Provider::~Provider() {}
73 
asProvider(const SkShaderBase::ContextRec & rec,SkArenaAlloc * alloc) const74 SkNormalSource::Provider* SkNormalFlatSourceImpl::asProvider(const SkShaderBase::ContextRec &rec,
75                                                              SkArenaAlloc *alloc) const {
76     return alloc->make<Provider>();
77 }
78 
fillScanLine(int x,int y,SkPoint3 output[],int count) const79 void SkNormalFlatSourceImpl::Provider::fillScanLine(int x, int y, SkPoint3 output[],
80                                                     int count) const {
81     for (int i = 0; i < count; i++) {
82         output[i] = {0.0f, 0.0f, 1.0f};
83     }
84 }
85 
86 ////////////////////////////////////////////////////////////////////////////////
87 
CreateProc(SkReadBuffer & buf)88 sk_sp<SkFlattenable> SkNormalFlatSourceImpl::CreateProc(SkReadBuffer& buf) {
89     return sk_make_sp<SkNormalFlatSourceImpl>();
90 }
91 
flatten(SkWriteBuffer & buf) const92 void SkNormalFlatSourceImpl::flatten(SkWriteBuffer& buf) const {
93     this->INHERITED::flatten(buf);
94 }
95 
96 ////////////////////////////////////////////////////////////////////////////
97 
MakeFlat()98 sk_sp<SkNormalSource> SkNormalSource::MakeFlat() {
99     return sk_make_sp<SkNormalFlatSourceImpl>();
100 }
101