1 #ifndef __DALI_INTERNAL_SCENE_GRAPH_IMAGE_ATTACHMENT_H__
2 #define __DALI_INTERNAL_SCENE_GRAPH_IMAGE_ATTACHMENT_H__
5 * Copyright (c) 2014 Samsung Electronics Co., Ltd.
7 * Licensed under the Apache License, Version 2.0 (the "License");
8 * you may not use this file except in compliance with the License.
9 * You may obtain a copy of the License at
11 * http://www.apache.org/licenses/LICENSE-2.0
13 * Unless required by applicable law or agreed to in writing, software
14 * distributed under the License is distributed on an "AS IS" BASIS,
15 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 * See the License for the specific language governing permissions and
17 * limitations under the License.
22 #include <dali/public-api/actors/image-actor.h>
23 #include <dali/public-api/math/rect.h>
24 #include <dali/public-api/shader-effects/shader-effect.h>
25 #include <dali/internal/event/common/event-thread-services.h>
26 #include <dali/internal/update/node-attachments/scene-graph-renderable-attachment.h>
27 #include <dali/internal/update/resources/bitmap-metadata.h>
35 // value types used by messages
36 template <> struct ParameterType< Dali::ImageActor::Style >
37 : public BasicType< Dali::ImageActor::Style > {};
46 * An attachment for rendering images in various ways.
48 class ImageAttachment : public RenderableAttachment
52 typedef Dali::ImageActor::Style Style;
53 typedef Dali::ImageActor::PixelArea PixelArea;
56 * Create a new ImageAttachment.
57 * @param [in] textureId The resource ID of a texture.
58 * @return The newly allocated ImageAttachment.
60 static ImageAttachment* New( unsigned int textureId );
65 virtual ~ImageAttachment();
68 * @copydoc RenderableAttachment::GetRenderer().
70 virtual Renderer& GetRenderer();
73 * @copydoc RenderableAttachment::GetRenderer().
75 virtual const Renderer& GetRenderer() const;
78 * Set the ID used to retrieve a texture from ResourceManager.
79 * @param[in] updateBufferIndex The current update buffer index.
80 * @param[in] textureId The texture ID.
82 void SetTextureId( BufferIndex updateBufferIndex, unsigned int textureId );
85 * Set the area of the texture to display.
86 * @param[in] updateBufferIndex The current update buffer index.
87 * @param [in] pixelArea The area to display, in pixels relative to the top-left (0,0) of the image.
89 void SetPixelArea( BufferIndex updateBufferIndex, const PixelArea& pixelArea );
92 * Query whether a pixel area has been set.
93 * @return True if a pixel area has been set.
95 bool IsPixelAreaSet() const
97 return mIsPixelAreaSet;
101 * Remove any pixel areas specified with SetPixelArea; the entire image will be displayed.
102 * @pre image must be initialized.
104 void ClearPixelArea();
107 * Set how the attachment is rendered; the default is STYLE_QUAD.
108 * @param [in] style The new style.
110 void SetStyle( Style style );
113 * Retrieve how the attachment is rendered.
122 * Set the border; this is applied with STYLE_NINE_PATCH.
123 * @param [in] updateBufferIndex to use.
124 * @param [in] border The new border setting.
125 * @param [in] inPixels if the border is in pixels.
127 void SetBorder( BufferIndex updateBufferIndex, const Vector4& border, bool inPixels );
130 * Set the blending options. This should only be called from the update-thread.
131 * @param[in] updateBufferIndex The current update buffer index.
132 * @param[in] options A bitmask of blending options.
134 void SetBlendingOptions( BufferIndex updateBufferIndex, unsigned int options );
137 * Set the blend-color. This should only be called from the update-thread.
138 * @param[in] updateBufferIndex The current update buffer index.
139 * @param[in] color The new blend-color.
141 void SetBlendColor( BufferIndex updateBufferIndex, const Vector4& color );
144 * Set the face-culling mode.
145 * @param[in] updateBufferIndex The current update buffer index.
146 * @param[in] mode The face-culling mode.
148 void SetCullFace( BufferIndex updateBufferIndex, CullFaceMode mode );
151 * Set the sampler used to render the texture for this renderable.
152 * @param[in] updateBufferIndex The current update buffer index.
153 * @param[in] samplerBitfield The image sampler packed options to set.
155 void SetSampler( BufferIndex updateBufferIndex, unsigned int samplerBitfield );
158 * Apply a shader on the renderable
159 * @param[in] updateBufferIndex The current update buffer index.
160 * @param[in] shader to apply.
162 void ApplyShader( BufferIndex updateBufferIndex, Shader* shader );
165 * Remove the shader from the renderable
166 * @param[in] updateBufferIndex The current update buffer index.
168 void RemoveShader( BufferIndex updateBufferIndex );
171 * Called to notify that the shader might have been changed
172 * The implementation should recalculate geometry and scale based on the
173 * hints from the new shader
174 * @param[in] updateBufferIndex The current update buffer index.
175 * @return Return true if the geometry changed.
177 virtual void ShaderChanged( BufferIndex updateBufferIndex );
180 * @copydoc RenderableAttachment::SizeChanged()
182 virtual void SizeChanged( BufferIndex updateBufferIndex );
185 * @copydoc RenderableAttachment::DoPrepareRender()
187 virtual void DoPrepareRender( BufferIndex updateBufferIndex );
190 * @copydoc RenderableAttachment::IsFullyOpaque()
192 virtual bool IsFullyOpaque( BufferIndex updateBufferIndex );
197 * Protected constructor
199 ImageAttachment( unsigned int textureId );
203 * Sends the shader to the renderer
204 * @param updateBufferIndex for the message buffer
206 void SendShaderChangeMessage( BufferIndex updateBufferIndex );
209 * @copydoc RenderableAttachment::Initialize2().
211 virtual void Initialize2( BufferIndex updateBufferIndex );
214 * @copydoc RenderableAttachment::OnDestroy2().
216 virtual void OnDestroy2();
219 * @copydoc NodeAttachment::ConnectedToSceneGraph()
221 virtual void ConnectedToSceneGraph();
224 * @copydoc NodeAttachment::DisconnectedFromSceneGraph()
226 virtual void DisconnectedFromSceneGraph();
229 * @copydoc RenderableAttachment::DoPrepareResources()
231 virtual bool DoPrepareResources( BufferIndex updateBufferIndex, ResourceManager& resourceManager );
233 // Helper to check whether a geometry hint was set
234 bool PreviousHintEnabled( Dali::ShaderEffect::GeometryHints hint ) const
236 return mPreviousRefreshHints & hint;
240 ImageAttachment(const ImageAttachment&);
243 ImageAttachment& operator=(const ImageAttachment& rhs);
247 ImageRenderer* mImageRenderer; ///< Raw-pointers to renderer that is owned by RenderManager
248 unsigned int mTextureId; ///< The resource ID for a texture
250 // bitfields to fit in single byte
251 bool mRefreshMeshData : 1; ///< Whether the vertex/index buffers needs regenerating
252 bool mIsPixelAreaSet : 1; ///< Whether pixel area is set, cached for image actor to be able to ask for it
253 int mPreviousRefreshHints : 4; ///< The shader geometry hints, when the vertex buffer was last refreshed, 4 bits is enough as there's 4 flags
254 Style mStyle : 2; ///< rendering style, 2 bits is enough as only 2 values in the enum
255 CullFaceMode mCullFaceMode : 3; ///< Cullface mode, 3 bits is enough for 4 values
257 BitmapMetadata mBitmapMetadata;///< The bitmap metadata
258 Vector2 mGeometrySize; ///< The size of the currently used geometry
262 // Messages for ImageAttachment
264 inline void SetTextureIdMessage( EventThreadServices& eventThreadServices, const ImageAttachment& attachment, unsigned int id )
266 typedef MessageDoubleBuffered1< ImageAttachment, unsigned int > LocalType;
268 // Reserve some memory inside the message queue
269 unsigned int* slot = eventThreadServices.ReserveMessageSlot( sizeof( LocalType ) );
271 // Construct message in the message queue memory; note that delete should not be called on the return value
272 new (slot) LocalType( &attachment, &ImageAttachment::SetTextureId, id );
275 inline void SetPixelAreaMessage( EventThreadServices& eventThreadServices, const ImageAttachment& attachment, const Dali::ImageActor::PixelArea& area )
277 typedef MessageDoubleBuffered1< ImageAttachment, Dali::ImageActor::PixelArea > LocalType;
279 // Reserve some memory inside the message queue
280 unsigned int* slot = eventThreadServices.ReserveMessageSlot( sizeof( LocalType ) );
282 // Construct message in the message queue memory; note that delete should not be called on the return value
283 new (slot) LocalType( &attachment, &ImageAttachment::SetPixelArea, area );
286 inline void ClearPixelAreaMessage( EventThreadServices& eventThreadServices, const ImageAttachment& attachment )
288 typedef Message< ImageAttachment > LocalType;
290 // Reserve some memory inside the message queue
291 unsigned int* slot = eventThreadServices.ReserveMessageSlot( sizeof( LocalType ) );
293 // Construct message in the message queue memory; note that delete should not be called on the return value
294 new (slot) LocalType( &attachment, &ImageAttachment::ClearPixelArea );
297 inline void SetStyleMessage( EventThreadServices& eventThreadServices, const ImageAttachment& attachment, Dali::ImageActor::Style style )
299 typedef MessageValue1< ImageAttachment, Dali::ImageActor::Style > LocalType;
301 // Reserve some memory inside the message queue
302 unsigned int* slot = eventThreadServices.ReserveMessageSlot( sizeof( LocalType ) );
304 // Construct message in the message queue memory; note that delete should not be called on the return value
305 new (slot) LocalType( &attachment, &ImageAttachment::SetStyle, style );
308 inline void SetNinePatchBorderMessage( EventThreadServices& eventThreadServices, const ImageAttachment& attachment, const Vector4& border, bool inPixels )
310 typedef MessageDoubleBuffered2< ImageAttachment, Vector4, bool > LocalType;
312 // Reserve some memory inside the message queue
313 unsigned int* slot = eventThreadServices.ReserveMessageSlot( sizeof( LocalType ) );
315 // Construct message in the message queue memory; note that delete should not be called on the return value
316 new (slot) LocalType( &attachment, &ImageAttachment::SetBorder, border, inPixels );
319 inline void SetSortModifierMessage( EventThreadServices& eventThreadServices, const ImageAttachment& attachment, float modifier )
321 typedef MessageValue1< ImageAttachment, float > LocalType;
323 // Reserve some memory inside the message queue
324 unsigned int* slot = eventThreadServices.ReserveMessageSlot( sizeof( LocalType ) );
326 // Construct message in the message queue memory; note that delete should not be called on the return value
327 new (slot) LocalType( &attachment, &ImageAttachment::SetSortModifier, modifier );
330 inline void SetCullFaceMessage( EventThreadServices& eventThreadServices, const ImageAttachment& attachment, CullFaceMode mode )
332 typedef MessageDoubleBuffered1< ImageAttachment, CullFaceMode > LocalType;
334 // Reserve some memory inside the message queue
335 unsigned int* slot = eventThreadServices.ReserveMessageSlot( sizeof( LocalType ) );
337 // Construct message in the message queue memory; note that delete should not be called on the return value
338 new (slot) LocalType( &attachment, &ImageAttachment::SetCullFace, mode );
341 inline void SetBlendingOptionsMessage( EventThreadServices& eventThreadServices, const ImageAttachment& attachment, unsigned int options )
343 typedef MessageDoubleBuffered1< ImageAttachment, unsigned int > LocalType;
345 // Reserve some memory inside the message queue
346 unsigned int* slot = eventThreadServices.ReserveMessageSlot( sizeof( LocalType ) );
348 new (slot) LocalType( &attachment, &ImageAttachment::SetBlendingOptions, options );
351 inline void SetBlendColorMessage( EventThreadServices& eventThreadServices, const ImageAttachment& attachment, const Vector4& color )
353 typedef MessageDoubleBuffered1< ImageAttachment, Vector4 > LocalType;
355 // Reserve some memory inside the message queue
356 unsigned int* slot = eventThreadServices.ReserveMessageSlot( sizeof( LocalType ) );
358 new (slot) LocalType( &attachment, &ImageAttachment::SetBlendColor, color );
361 inline void SetSamplerMessage( EventThreadServices& eventThreadServices, const ImageAttachment& attachment, unsigned int samplerBitfield )
363 typedef MessageDoubleBuffered1< ImageAttachment, unsigned int > LocalType;
365 // Reserve some memory inside the message queue
366 unsigned int* slot = eventThreadServices.ReserveMessageSlot( sizeof( LocalType ) );
368 // Construct message in the message queue memory; note that delete should not be called on the return value
369 new (slot) LocalType( &attachment, &ImageAttachment::SetSampler, samplerBitfield );
372 inline void ApplyShaderMessage( EventThreadServices& eventThreadServices, const ImageAttachment& attachment, const Shader& constShader )
374 // Update thread can edit the object
375 Shader& shader = const_cast< Shader& >( constShader );
377 typedef MessageDoubleBuffered1< ImageAttachment, Shader* > LocalType;
379 // Reserve some memory inside the message queue
380 unsigned int* slot = eventThreadServices.ReserveMessageSlot( sizeof( LocalType ) );
382 // Construct message in the message queue memory; note that delete should not be called on the return value
383 new (slot) LocalType( &attachment, &ImageAttachment::ApplyShader, &shader );
386 inline void RemoveShaderMessage( EventThreadServices& eventThreadServices, const ImageAttachment& attachment )
388 typedef MessageDoubleBuffered0< ImageAttachment > LocalType;
390 // Reserve some memory inside the message queue
391 unsigned int* slot = eventThreadServices.ReserveMessageSlot( sizeof( LocalType ) );
393 // Construct message in the message queue memory; note that delete should not be called on the return value
394 new (slot) LocalType( &attachment, &ImageAttachment::RemoveShader );
397 } // namespace SceneGraph
399 } // namespace Internal
403 #endif // __DALI_INTERNAL_SCENE_GRAPH_IMAGE_ATTACHMENT_H__