Use modern construct 'using' instead of typedef.
[platform/core/uifw/dali-core.git] / dali / internal / event / effects / shader-factory.h
index 6da5ca4..c09f859 100644 (file)
@@ -1,8 +1,8 @@
-#ifndef __DALI_INTERNAL_SHADER_FACTORY_H__
-#define __DALI_INTERNAL_SHADER_FACTORY_H__
+#ifndef DALI_INTERNAL_SHADER_FACTORY_H
+#define DALI_INTERNAL_SHADER_FACTORY_H
 
 /*
- * Copyright (c) 2014 Samsung Electronics Co., Ltd.
+ * Copyright (c) 2019 Samsung Electronics Co., Ltd.
  *
  * Licensed under the Apache License, Version 2.0 (the "License");
  * you may not use this file except in compliance with the License.
  */
 
 // INTERNAL INCLUDES
-#include <dali/internal/event/resources/resource-ticket.h>
-#include <dali/internal/event/resources/resource-type-path-id-map.h>
-#include <dali/internal/event/effects/shader-declarations.h>
+#include <dali/public-api/common/dali-vector.h>
+#include <dali/internal/common/message.h>
+#include <dali/internal/common/shader-data.h>
+#include <dali/internal/common/shader-saver.h>
 
 namespace Dali
 {
@@ -29,77 +30,79 @@ namespace Dali
 namespace Internal
 {
 
-class ResourceClient;
-class MessageController;
+class ShaderData;
+using ShaderDataPtr = IntrusivePtr<ShaderData>;
 
 /**
- * ShaderFactory is an object that manages shader binary resource load requests,
- * It triggers the load requests during core initialization and sends a message to the
- * render manager with information about all the requested shader binaries and their
- * request Id.
+ * @brief ShaderFactory loads and saves shader binaries synchronously.
+ *
+ * Binaries loaded or saved are also cached by the ShaderFactory.
  */
-class ShaderFactory
+class ShaderFactory : public ShaderSaver
 {
 public:
 
   /**
-   * default constructor
+   * Default constructor
    */
-  ShaderFactory(ResourceClient& resourceClient);
+  ShaderFactory();
 
   /**
-   * Default destructor
+   * Destructor
    */
   virtual ~ShaderFactory();
 
   /**
-   * Issues a request to load a binary version of a shader program, and returns a resource ticket
-   * If a request for an identical shader has already been made, the ticket for the older request
-   * is shared.
+   * @brief Looks for precompiled binary version of shader program in memory and file caches.
+   *
+   * Tries to load a binary version of a shader program identified by a hash over the two source
+   * files, checking an in-memory cache first.
+   * If the cache hits or the load succeeds, the buffer member of the returned ShaderData will
+   * contain a precompiled shader binary program which can be uploaded directly to GLES.
+   *
    * @param [in] vertexSource   The vertex shader source code
    * @param [in] fragmentSource The fragment shader source code
-   * @param [out] shaderHash  hash key created with vertex and fragment shader code
-   * @return                    A ticket for the resource
+   * @param [out] shaderHash    Hash key created from vertex and fragment shader code
+   * @return                    ShaderData containing the source and hash value, and additionally,
+   *                            a compiled shader program binary if one could be found, else an
+   *                            empty binary buffer cleared to size zero.
    */
-  ResourceTicketPtr Load( const std::string& vertexSource, const std::string& fragmentSource, size_t& shaderHash );
+  Internal::ShaderDataPtr Load( const std::string& vertexSource, const std::string& fragmentSource, const Dali::Shader::Hint::Value hints, size_t& shaderHash );
 
   /**
-   * Called during Core initialization to load the default shader.
+   * @brief Saves shader to memory cache and filesystem.
+   *
+   * This is called when a shader binary is ready to be saved to the memory cache file system.
+   * Shaders that pass through here become available to subsequent invocations of Load.
+   * @param[in] shader The data to be saved.
+   * @sa Load
    */
-  void LoadDefaultShaders();
-
-  /**
-   * Called during ShaderEffect::New() to load shaders for text sub-types
-   * (Default shader can be overridden)
-   * @param[in] shaderEffect The shader effect for which to load the sub-type shaders
-   */
-  static void LoadTextSubtypeShaders(ShaderEffectPtr shaderEffect);
+  virtual void SaveBinary( Internal::ShaderDataPtr shader );
 
 private:
 
+  void MemoryCacheInsert( Internal::ShaderData& shaderData );
+
   // Undefined
   ShaderFactory( const ShaderFactory& );
 
   // Undefined
   ShaderFactory& operator=( const ShaderFactory& rhs );
 
-  /**
-   * Returns a hash of the shader strings
-   * @param[in] vertexShaderSrc   Source code for the vertex shader
-   * @param[in] fragmentShaderSrc Source code for the fragment shader
-   * @return Hashed value
-   */
-  size_t HashShaderSource( const std::string& vertexShaderSrc, const std::string& fragmentShaderSrc ) const;
-
 private:
-  ResourceClient&       mResourceClient;
-  ResourceTypePathIdMap mResourceTypePathIdMap; ///< A map of resource IDs sorted by ResourceTypePath
-  ShaderEffectPtr       mDefaultShader;
+  Dali::Vector< Internal::ShaderData* > mShaderBinaryCache; ///< Cache of pre-compiled shaders.
 
 }; // class ShaderFactory
 
+inline MessageBase* ShaderCompiledMessage( ShaderSaver& factory, Internal::ShaderDataPtr shaderData )
+{
+  return new MessageValue1< ShaderSaver, Internal::ShaderDataPtr >( &factory,
+                                                            &ShaderSaver::SaveBinary,
+                                                            shaderData );
+}
+
 } // namespace Internal
 
 } // namespace Dali
 
-#endif // __DALI_INTERNAL_SHADER_FACTORY_H__
+#endif // DALI_INTERNAL_SHADER_FACTORY_H