| 1 | /* | 
|---|
| 2 | * Copyright 2019 Google LLC | 
|---|
| 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 | #ifndef SKSL_EXTERNALVALUE | 
|---|
| 9 | #define SKSL_EXTERNALVALUE | 
|---|
| 10 |  | 
|---|
| 11 | #include "src/sksl/ir/SkSLSymbol.h" | 
|---|
| 12 |  | 
|---|
| 13 | namespace SkSL { | 
|---|
| 14 |  | 
|---|
| 15 | class String; | 
|---|
| 16 | class Type; | 
|---|
| 17 |  | 
|---|
| 18 | class ExternalValue : public Symbol { | 
|---|
| 19 | public: | 
|---|
| 20 | ExternalValue(const char* name, const Type& type) | 
|---|
| 21 | : INHERITED(-1, kExternal_Kind, name) | 
|---|
| 22 | , fType(type) {} | 
|---|
| 23 |  | 
|---|
| 24 | virtual bool canRead() const { | 
|---|
| 25 | return false; | 
|---|
| 26 | } | 
|---|
| 27 |  | 
|---|
| 28 | virtual bool canWrite() const { | 
|---|
| 29 | return false; | 
|---|
| 30 | } | 
|---|
| 31 |  | 
|---|
| 32 | virtual bool canCall() const { | 
|---|
| 33 | return false; | 
|---|
| 34 | } | 
|---|
| 35 |  | 
|---|
| 36 | /** | 
|---|
| 37 | * Returns the type for purposes of read and write operations. | 
|---|
| 38 | */ | 
|---|
| 39 | virtual const Type& type() const { | 
|---|
| 40 | return fType; | 
|---|
| 41 | } | 
|---|
| 42 |  | 
|---|
| 43 | virtual int callParameterCount() const { | 
|---|
| 44 | return -1; | 
|---|
| 45 | } | 
|---|
| 46 |  | 
|---|
| 47 | /** | 
|---|
| 48 | * Fills in the outTypes array with pointers to the parameter types. outTypes must be able to | 
|---|
| 49 | * hold callParameterCount() pointers. | 
|---|
| 50 | */ | 
|---|
| 51 | virtual void getCallParameterTypes(const Type** outTypes) const { | 
|---|
| 52 | SkASSERT(false); | 
|---|
| 53 | } | 
|---|
| 54 |  | 
|---|
| 55 | /** | 
|---|
| 56 | * Returns the return type resulting from a call operation. | 
|---|
| 57 | */ | 
|---|
| 58 | virtual const Type& callReturnType() const { | 
|---|
| 59 | return fType; | 
|---|
| 60 | } | 
|---|
| 61 |  | 
|---|
| 62 | /** | 
|---|
| 63 | * Reads the external value and stores the resulting data in target. The caller must ensure | 
|---|
| 64 | * that target is a valid pointer to a region of sufficient size to hold the data contained | 
|---|
| 65 | * in this external value. | 
|---|
| 66 | * 'index' is the element index ([0 .. N-1]) within a call to ByteCode::run() | 
|---|
| 67 | */ | 
|---|
| 68 | virtual void read(int index, float* target) { | 
|---|
| 69 | SkASSERT(false); | 
|---|
| 70 | } | 
|---|
| 71 |  | 
|---|
| 72 | /** | 
|---|
| 73 | * Copies the value in src into this external value. The caller must ensure that src is a | 
|---|
| 74 | * pointer to the type of data expected by this external value. | 
|---|
| 75 | * 'index' is the element index ([0 .. N-1]) within a call to ByteCode::run() | 
|---|
| 76 | */ | 
|---|
| 77 | virtual void write(int index, float* src) { | 
|---|
| 78 | SkASSERT(false); | 
|---|
| 79 | } | 
|---|
| 80 |  | 
|---|
| 81 | /** | 
|---|
| 82 | * Calls the value as a function with the specified parameters. arguments must be a pointer to | 
|---|
| 83 | * a structure containing the arguments expected by the external value in source order, and | 
|---|
| 84 | * outResult must be a pointer to a region of sufficient size to hold the function's return | 
|---|
| 85 | * value. | 
|---|
| 86 | * 'index' is the element index ([0 .. N-1]) within a call to ByteCode::run() | 
|---|
| 87 | */ | 
|---|
| 88 | virtual void call(int index, float* arguments, float* outResult) { | 
|---|
| 89 | SkASSERT(false); | 
|---|
| 90 | } | 
|---|
| 91 |  | 
|---|
| 92 | /** | 
|---|
| 93 | * Resolves 'name' within this context and returns an ExternalValue which represents it, or | 
|---|
| 94 | * null if no such child exists. If the implementation of this method creates new | 
|---|
| 95 | * ExternalValues and there isn't a more convenient place for ownership of the objects to | 
|---|
| 96 | * reside, the compiler's takeOwnership method may be useful. | 
|---|
| 97 | * | 
|---|
| 98 | * The 'name' string may not persist after this call; do not store this pointer. | 
|---|
| 99 | */ | 
|---|
| 100 | virtual ExternalValue* getChild(const char* name) const { | 
|---|
| 101 | return nullptr; | 
|---|
| 102 | } | 
|---|
| 103 |  | 
|---|
| 104 | String description() const override { | 
|---|
| 105 | return String( "external<") + fName + ">"; | 
|---|
| 106 | } | 
|---|
| 107 |  | 
|---|
| 108 | private: | 
|---|
| 109 | typedef Symbol INHERITED; | 
|---|
| 110 |  | 
|---|
| 111 | const Type& fType; | 
|---|
| 112 | }; | 
|---|
| 113 |  | 
|---|
| 114 | }  // namespace SkSL | 
|---|
| 115 |  | 
|---|
| 116 | #endif | 
|---|
| 117 |  | 
|---|