1
2/*
3 * Copyright 2011 Google Inc.
4 *
5 * Use of this source code is governed by a BSD-style license that can be
6 * found in the LICENSE file.
7 */
8
9
10#ifndef SkIStream_DEFINED
11#define SkIStream_DEFINED
12
13#include "SkTypes.h"
14
15#ifdef SK_BUILD_FOR_WIN
16
17#include "SkLeanWindows.h"
18#include <ole2.h>
19
20class SkStream;
21class SkWStream;
22
23/**
24 * A bare IStream implementation which properly reference counts
25 * but returns E_NOTIMPL for all ISequentialStream and IStream methods.
26 */
27class SkBaseIStream : public IStream {
28private:
29    LONG _refcount;
30
31protected:
32    explicit SkBaseIStream();
33    virtual ~SkBaseIStream();
34
35public:
36    virtual HRESULT STDMETHODCALLTYPE QueryInterface(REFIID iid
37                                                   , void ** ppvObject);
38    virtual ULONG STDMETHODCALLTYPE AddRef(void);
39    virtual ULONG STDMETHODCALLTYPE Release(void);
40
41    // ISequentialStream Interface
42public:
43    virtual HRESULT STDMETHODCALLTYPE Read(void* pv, ULONG cb, ULONG* pcbRead);
44
45    virtual HRESULT STDMETHODCALLTYPE Write(void const* pv
46                                          , ULONG cb
47                                          , ULONG* pcbWritten);
48
49    // IStream Interface
50public:
51    virtual HRESULT STDMETHODCALLTYPE SetSize(ULARGE_INTEGER);
52
53    virtual HRESULT STDMETHODCALLTYPE CopyTo(IStream*
54                                           , ULARGE_INTEGER
55                                           , ULARGE_INTEGER*
56                                           , ULARGE_INTEGER*);
57
58    virtual HRESULT STDMETHODCALLTYPE Commit(DWORD);
59
60    virtual HRESULT STDMETHODCALLTYPE Revert(void);
61
62    virtual HRESULT STDMETHODCALLTYPE LockRegion(ULARGE_INTEGER
63                                               , ULARGE_INTEGER
64                                               , DWORD);
65
66    virtual HRESULT STDMETHODCALLTYPE UnlockRegion(ULARGE_INTEGER
67                                                 , ULARGE_INTEGER
68                                                 , DWORD);
69
70    virtual HRESULT STDMETHODCALLTYPE Clone(IStream **);
71
72    virtual HRESULT STDMETHODCALLTYPE Seek(LARGE_INTEGER liDistanceToMove
73                                         , DWORD dwOrigin
74                                         , ULARGE_INTEGER* lpNewFilePointer);
75
76    virtual HRESULT STDMETHODCALLTYPE Stat(STATSTG* pStatstg
77                                         , DWORD grfStatFlag);
78};
79
80/**
81 * A minimal read-only IStream implementation which wraps an SkStream.
82 */
83class SkIStream : public SkBaseIStream {
84private:
85    SkStream *fSkStream;
86    const bool fDeleteOnRelease;
87    ULARGE_INTEGER fLocation;
88
89    SkIStream(SkStream* stream, bool fDeleteOnRelease);
90    virtual ~SkIStream();
91
92public:
93    HRESULT static CreateFromSkStream(SkStream* stream
94                                    , bool fDeleteOnRelease
95                                    , IStream ** ppStream);
96
97    virtual HRESULT STDMETHODCALLTYPE Read(void* pv, ULONG cb, ULONG* pcbRead);
98
99    virtual HRESULT STDMETHODCALLTYPE Write(void const* pv
100                                          , ULONG cb
101                                          , ULONG* pcbWritten);
102
103    virtual HRESULT STDMETHODCALLTYPE Seek(LARGE_INTEGER liDistanceToMove
104                                         , DWORD dwOrigin
105                                         , ULARGE_INTEGER* lpNewFilePointer);
106
107    virtual HRESULT STDMETHODCALLTYPE Stat(STATSTG* pStatstg
108                                         , DWORD grfStatFlag);
109};
110
111/**
112 * A minimal write-only IStream implementation which wraps an SkWIStream.
113 */
114class SkWIStream : public SkBaseIStream {
115private:
116    SkWStream *fSkWStream;
117
118    SkWIStream(SkWStream* stream);
119    virtual ~SkWIStream();
120
121public:
122    HRESULT static CreateFromSkWStream(SkWStream* stream, IStream ** ppStream);
123
124    virtual HRESULT STDMETHODCALLTYPE Write(void const* pv
125                                          , ULONG cb
126                                          , ULONG* pcbWritten);
127
128    virtual HRESULT STDMETHODCALLTYPE Commit(DWORD);
129
130    virtual HRESULT STDMETHODCALLTYPE Stat(STATSTG* pStatstg
131                                         , DWORD grfStatFlag);
132};
133
134#endif  // SK_BUILD_FOR_WIN
135#endif  // SkIStream_DEFINED
136