CompositionDrawingSurface CompositionDrawingSurface CompositionDrawingSurface CompositionDrawingSurface Class


A drawing surface for interoperation with Direct2D or Direct3D.

public : class CompositionDrawingSurface : CompositionObject, ICompositionDrawingSurface, ICompositionDrawingSurface2, ICompositionSurface
struct winrt::Windows::UI::Composition::CompositionDrawingSurface : CompositionObject, ICompositionDrawingSurface, ICompositionDrawingSurface2, ICompositionSurface
public class CompositionDrawingSurface : CompositionObject, ICompositionDrawingSurface, ICompositionDrawingSurface2, ICompositionSurface
Public Class CompositionDrawingSurface Inherits CompositionObject Implements ICompositionDrawingSurface, ICompositionDrawingSurface2, ICompositionSurface
Windows 10 requirements
Device family
Windows 10 (introduced v10.0.10586.0)
API contract
Windows.Foundation.UniversalApiContract (introduced v2)


#include "stdafx.h"

using namespace Microsoft::WRL;
using namespace Windows::Foundation;
using namespace Windows::Graphics::DirectX;
using namespace Windows::UI::Composition;

// This is an app-provided helper to render lines of text
class SampleText
    // The text to draw
    ComPtr<IDWriteTextLayout> _text;

    // The composition surface that we use in the visual tree
    ComPtr<ICompositionDrawingSurfaceInterop> _drawingSurfaceInterop;

    // The device that owns the surface
    ComPtr<ICompositionGraphicsDevice> _compositionGraphicsDevice;

    // For managing our event notifier
    EventRegistrationToken _deviceReplacedEventToken;

    SampleText(IDWriteTextLayout* text, ICompositionGraphicsDevice* compositionGraphicsDevice) :
        // Create the surface just big enough to hold the formatted text block.
        DWRITE_TEXT_METRICS metrics;
        Windows::Foundation::Size surfaceSize = { metrics.width, metrics.height };
        ComPtr<ICompositionDrawingSurface> drawingSurface = _compositionGraphicsDevice->CreateDrawingSurface(

        // Cache the interop pointer, since that's what we always use.

        // Draw the text

        // If the rendering device is lost, the application will recreate and replace it. We then
        // own redrawing our pixels.
                ICompositionGraphicsDevice* source, IRenderingDeviceReplacedEventArgs* args)
                -> HRESULT
                // Draw the text again
                return S_OK;


    // Return the underlying surface to the caller
    ComPtr<ICompositionSurface> get_Surface()
        // To the caller, the fact that we have a drawing surface is an implementation detail.
        // Return the base interface instead
        ComPtr<ICompositionSurface> surface;
        return surface;

    // We may detect device loss on BeginDraw calls. This helper handles this condition or other
    // errors.
    bool CheckForDeviceRemoved(HRESULT hr)
        if (SUCCEEDED(hr))
            // Everything is fine -- go ahead and draw
            return true;
        else if (hr == DXGI_ERROR_DEVICE_REMOVED)
            // We can't draw at this time, but this failure is recoverable. Just skip drawing for
            // now. We will be asked to draw again once the Direct3D device is recreated
            return false;
            // Any other error is unexpected and, therefore, fatal

    // Renders the text into our composition surface
    void DrawText()
        // Begin our update of the surface pixels. If this is our first update, we are required
        // to specify the entire surface, which nullptr is shorthand for (but, as it works out,
        // any time we make an update we touch the entire surface, so we always pass nullptr).
        ComPtr<ID2D1DeviceContext> d2dDeviceContext;
        POINT offset;
        if (CheckForDeviceRemoved(_drawingSurfaceInterop->BeginDraw(nullptr,
            __uuidof(ID2D1DeviceContext), &d2dDeviceContext, &offset)))
            // Create a solid color brush for the text. A more sophisticated application might want
            // to cache and reuse a brush across all text elements instead, taking care to recreate
            // it in the event of device removed.
            ComPtr<ID2D1SolidColorBrush> brush;
                D2D1::ColorF(D2D1::ColorF::Black, 1.0f), &brush));

            // Draw the line of text at the specified offset, which corresponds to the top-left
            // corner of our drawing surface. Notice we don't call BeginDraw on the D2D device
            // context; this has already been done for us by the composition API.
            d2dDeviceContext->DrawTextLayout(D2D1::Point2F(offset.x, offset.y), _text.Get(),

            // Our update is done. EndDraw never indicates rendering device removed, so any
            // failure here is unexpected and, therefore, fatal.

class SampleApp
    ComPtr<ICompositor> _compositor;
    ComPtr<ID2D1Device> _d2dDevice;
    ComPtr<ICompositionGraphicsDevice> _compositionGraphicsDevice;
    std::vector<ComPtr<SampleText>> _textSurfaces;

    // Run once when the application starts up
    void Initialize(ICompositor* compositor)
        // Cache the compositor (created outside of this method)
        _compositor = compositor;

        // Create a Direct2D device (helper implementation not shown here)

        // To create a composition graphics device, we need to QI for another interface
        ComPtr<ICompositorInterop> compositorInterop;

        // Create a graphics device backed by our D3D device

    // Called when Direct3D signals the device lost event
    void OnDirect3DDeviceLost()
        // Create a new device

        // Restore our composition graphics device to good health
        ComPtr<ICompositionGraphicsDeviceInterop> compositionGraphicsDeviceInterop;

    // Create a surface that is asynchronously filled with an image
    ComPtr<ICompositionSurface> CreateSurfaceFromTextLayout(IDWriteTextLayout* text)
        // Create our wrapper object that will handle downloading and decoding the image (assume
        // throwing new here)
        SampleText* textSurface = new SampleText(text, _compositionGraphicsDevice.Get());

        // Keep our image alive

        // The caller is only interested in the underlying surface
        return textSurface->get_Surface();

    // Create a visual that holds an image
    ComPtr<IVisual> CreateVisualFromTextLayout(IDWriteTextLayout* text)
        // Create a sprite visual
        ComPtr<ISpriteVisual> spriteVisual;

        // The sprite visual needs a brush to hold the image
        ComPtr<ICompositionSurfaceBrush> surfaceBrush;

        // Associate the brush with the visual
        ComPtr<ICompositionBrush> brush;

        // Return the visual to the caller as the base class
        ComPtr<IVisual> visual;

        return visual;

    // This helper (implementation not shown here) creates a Direct2D device and registers
    // for a device loss notification on the underlying Direct3D device. When that notification is
    // raised, assume the OnDirect3DDeviceLost method is called.
    HRESULT CreateDirect2DDevice(ID2D1Device** ppDevice);


AlphaMode AlphaMode AlphaMode AlphaMode

The alpha mode of the drawing surface.

Comment Comment Comment Comment

A string to associate with the CompositionObject.

(Inherited from CompositionObject)
Compositor Compositor Compositor Compositor

The Compositor used to create this CompositionObject.

(Inherited from CompositionObject)
Dispatcher Dispatcher Dispatcher Dispatcher

The dispatcher for the CompositionObject.

(Inherited from CompositionObject)
DispatcherQueue DispatcherQueue DispatcherQueue DispatcherQueue

Gets the DispatcherQueue for the CompostionObject.

(Inherited from CompositionObject)
ImplicitAnimations ImplicitAnimations ImplicitAnimations ImplicitAnimations

The collection of implicit animations attached to this object.

(Inherited from CompositionObject)
PixelFormat PixelFormat PixelFormat PixelFormat

The pixel format of the drawing surface.

Properties Properties Properties Properties

The collection of properties associated with the CompositionObject.

(Inherited from CompositionObject)
Size Size Size Size

The size of the drawing surface.

SizeInt32 SizeInt32 SizeInt32 SizeInt32

The size of the drawing surface.


Close Close Close Close

Closes the CompositionObject and releases system resources.

(Inherited from CompositionObject)
Resize(SizeInt32) Resize(SizeInt32) Resize(SizeInt32) Resize(SizeInt32)

Resizes the drawing surface to the specified size.

Scroll(PointInt32) Scroll(PointInt32) Scroll(PointInt32) Scroll(PointInt32)

Scrolls the drawing surface.

Scroll(PointInt32, RectInt32) Scroll(PointInt32, RectInt32) Scroll(PointInt32, RectInt32) Scroll(PointInt32, RectInt32)

Scrolls the drawing surface.

ScrollWithClip(PointInt32, RectInt32) ScrollWithClip(PointInt32, RectInt32) ScrollWithClip(PointInt32, RectInt32) ScrollWithClip(PointInt32, RectInt32)

Scrolls the drawing surface using the specified clip rectangle.

ScrollWithClip(PointInt32, RectInt32, RectInt32) ScrollWithClip(PointInt32, RectInt32, RectInt32) ScrollWithClip(PointInt32, RectInt32, RectInt32) ScrollWithClip(PointInt32, RectInt32, RectInt32)

Scrolls the drawing surface with the specified clip rectangle.

StartAnimation(String,CompositionAnimation) StartAnimation(String,CompositionAnimation) StartAnimation(String,CompositionAnimation) StartAnimation(String,CompositionAnimation)

Connects an animation with the specified property of the object and starts the animation.

(Inherited from CompositionObject)
StartAnimationGroup(ICompositionAnimationBase) StartAnimationGroup(ICompositionAnimationBase) StartAnimationGroup(ICompositionAnimationBase) StartAnimationGroup(ICompositionAnimationBase)

Starts an animation group.

The StartAnimationGroup method on CompositionObject lets you start CompositionAnimationGroup. All the animations in the group will be started at the same time on the object.

(Inherited from CompositionObject)
StopAnimation(String) StopAnimation(String) StopAnimation(String) StopAnimation(String)

Disconnects an animation from the specified property and stops the animation.

(Inherited from CompositionObject)
StopAnimationGroup(ICompositionAnimationBase) StopAnimationGroup(ICompositionAnimationBase) StopAnimationGroup(ICompositionAnimationBase) StopAnimationGroup(ICompositionAnimationBase)

Stops an animation group.

(Inherited from CompositionObject)
TryGetAnimationController(String) TryGetAnimationController(String) TryGetAnimationController(String) TryGetAnimationController(String)

Prerelease. Returns an AnimationController for the animation running on the specified property.

(Inherited from CompositionObject)

See Also