bungeman@google.com | 9df621d | 2011-06-23 21:43:52 +0000 | [diff] [blame] | 1 | /* |
epoger@google.com | ec3ed6a | 2011-07-28 14:26:00 +0000 | [diff] [blame] | 2 | * Copyright 2011 Google Inc. |
| 3 | * |
| 4 | * Use of this source code is governed by a BSD-style license that can be |
| 5 | * found in the LICENSE file. |
bungeman@google.com | 9df621d | 2011-06-23 21:43:52 +0000 | [diff] [blame] | 6 | */ |
| 7 | |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 8 | #include "include/core/SkTypes.h" |
Mike Klein | 8f11d4d | 2018-01-24 12:42:55 -0500 | [diff] [blame] | 9 | #if defined(SK_BUILD_FOR_WIN) |
epoger@google.com | ec3ed6a | 2011-07-28 14:26:00 +0000 | [diff] [blame] | 10 | |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 11 | #include "include/core/SkStream.h" |
| 12 | #include "src/utils/win/SkIStream.h" |
bungeman@google.com | 9df621d | 2011-06-23 21:43:52 +0000 | [diff] [blame] | 13 | |
| 14 | /** |
| 15 | * SkBaseIStream |
| 16 | */ |
| 17 | SkBaseIStream::SkBaseIStream() : _refcount(1) { } |
| 18 | SkBaseIStream::~SkBaseIStream() { } |
| 19 | |
Ben Wagner | 6cb6a07 | 2019-08-12 18:30:27 -0400 | [diff] [blame] | 20 | SK_STDMETHODIMP SkBaseIStream::QueryInterface(REFIID iid, void ** ppvObject) { |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 21 | if (nullptr == ppvObject) { |
bungeman@google.com | 9df621d | 2011-06-23 21:43:52 +0000 | [diff] [blame] | 22 | return E_INVALIDARG; |
| 23 | } |
| 24 | if (iid == __uuidof(IUnknown) |
| 25 | || iid == __uuidof(IStream) |
| 26 | || iid == __uuidof(ISequentialStream)) |
| 27 | { |
| 28 | *ppvObject = static_cast<IStream*>(this); |
| 29 | AddRef(); |
| 30 | return S_OK; |
| 31 | } else { |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 32 | *ppvObject = nullptr; |
rmistry@google.com | d6176b0 | 2012-08-23 18:14:13 +0000 | [diff] [blame] | 33 | return E_NOINTERFACE; |
bungeman@google.com | 9df621d | 2011-06-23 21:43:52 +0000 | [diff] [blame] | 34 | } |
| 35 | } |
| 36 | |
Ben Wagner | 6cb6a07 | 2019-08-12 18:30:27 -0400 | [diff] [blame] | 37 | SK_STDMETHODIMP_(ULONG) SkBaseIStream::AddRef() { |
bungeman@google.com | 9df621d | 2011-06-23 21:43:52 +0000 | [diff] [blame] | 38 | return (ULONG)InterlockedIncrement(&_refcount); |
| 39 | } |
| 40 | |
Ben Wagner | 6cb6a07 | 2019-08-12 18:30:27 -0400 | [diff] [blame] | 41 | SK_STDMETHODIMP_(ULONG) SkBaseIStream::Release() { |
bungeman@google.com | 9df621d | 2011-06-23 21:43:52 +0000 | [diff] [blame] | 42 | ULONG res = (ULONG) InterlockedDecrement(&_refcount); |
| 43 | if (0 == res) { |
| 44 | delete this; |
| 45 | } |
| 46 | return res; |
| 47 | } |
| 48 | |
| 49 | // ISequentialStream Interface |
Ben Wagner | 6cb6a07 | 2019-08-12 18:30:27 -0400 | [diff] [blame] | 50 | SK_STDMETHODIMP SkBaseIStream::Read(void* pv, ULONG cb, ULONG* pcbRead) |
bungeman@google.com | 9df621d | 2011-06-23 21:43:52 +0000 | [diff] [blame] | 51 | { return E_NOTIMPL; } |
| 52 | |
Ben Wagner | 6cb6a07 | 2019-08-12 18:30:27 -0400 | [diff] [blame] | 53 | SK_STDMETHODIMP SkBaseIStream::Write(void const* pv, ULONG cb, ULONG* pcbWritten) |
bungeman@google.com | 9df621d | 2011-06-23 21:43:52 +0000 | [diff] [blame] | 54 | { return E_NOTIMPL; } |
| 55 | |
| 56 | // IStream Interface |
Ben Wagner | 6cb6a07 | 2019-08-12 18:30:27 -0400 | [diff] [blame] | 57 | SK_STDMETHODIMP SkBaseIStream::SetSize(ULARGE_INTEGER) |
bungeman@google.com | 9df621d | 2011-06-23 21:43:52 +0000 | [diff] [blame] | 58 | { return E_NOTIMPL; } |
| 59 | |
Ben Wagner | 6cb6a07 | 2019-08-12 18:30:27 -0400 | [diff] [blame] | 60 | SK_STDMETHODIMP SkBaseIStream::CopyTo(IStream*, ULARGE_INTEGER, ULARGE_INTEGER*, ULARGE_INTEGER*) |
bungeman@google.com | 9df621d | 2011-06-23 21:43:52 +0000 | [diff] [blame] | 61 | { return E_NOTIMPL; } |
| 62 | |
Ben Wagner | 6cb6a07 | 2019-08-12 18:30:27 -0400 | [diff] [blame] | 63 | SK_STDMETHODIMP SkBaseIStream::Commit(DWORD) |
bungeman@google.com | 9df621d | 2011-06-23 21:43:52 +0000 | [diff] [blame] | 64 | { return E_NOTIMPL; } |
| 65 | |
Ben Wagner | 6cb6a07 | 2019-08-12 18:30:27 -0400 | [diff] [blame] | 66 | SK_STDMETHODIMP SkBaseIStream::Revert() |
bungeman@google.com | 9df621d | 2011-06-23 21:43:52 +0000 | [diff] [blame] | 67 | { return E_NOTIMPL; } |
| 68 | |
Ben Wagner | 6cb6a07 | 2019-08-12 18:30:27 -0400 | [diff] [blame] | 69 | SK_STDMETHODIMP SkBaseIStream::LockRegion(ULARGE_INTEGER, ULARGE_INTEGER, DWORD) |
bungeman@google.com | 9df621d | 2011-06-23 21:43:52 +0000 | [diff] [blame] | 70 | { return E_NOTIMPL; } |
| 71 | |
Ben Wagner | 6cb6a07 | 2019-08-12 18:30:27 -0400 | [diff] [blame] | 72 | SK_STDMETHODIMP SkBaseIStream::UnlockRegion(ULARGE_INTEGER, ULARGE_INTEGER, DWORD) |
bungeman@google.com | 9df621d | 2011-06-23 21:43:52 +0000 | [diff] [blame] | 73 | { return E_NOTIMPL; } |
| 74 | |
Ben Wagner | 6cb6a07 | 2019-08-12 18:30:27 -0400 | [diff] [blame] | 75 | SK_STDMETHODIMP SkBaseIStream::Clone(IStream**) |
bungeman@google.com | 9df621d | 2011-06-23 21:43:52 +0000 | [diff] [blame] | 76 | { return E_NOTIMPL; } |
| 77 | |
Ben Wagner | 6cb6a07 | 2019-08-12 18:30:27 -0400 | [diff] [blame] | 78 | SK_STDMETHODIMP SkBaseIStream::Seek(LARGE_INTEGER liDistanceToMove, |
| 79 | DWORD dwOrigin, |
| 80 | ULARGE_INTEGER* lpNewFilePointer) |
bungeman@google.com | 9df621d | 2011-06-23 21:43:52 +0000 | [diff] [blame] | 81 | { return E_NOTIMPL; } |
| 82 | |
Ben Wagner | 6cb6a07 | 2019-08-12 18:30:27 -0400 | [diff] [blame] | 83 | SK_STDMETHODIMP SkBaseIStream::Stat(STATSTG* pStatstg, DWORD grfStatFlag) |
bungeman@google.com | 9df621d | 2011-06-23 21:43:52 +0000 | [diff] [blame] | 84 | { return E_NOTIMPL; } |
| 85 | |
| 86 | |
| 87 | /** |
| 88 | * SkIStream |
| 89 | */ |
Ben Wagner | 11eae3d | 2019-08-22 17:40:34 -0400 | [diff] [blame] | 90 | SkIStream::SkIStream(std::unique_ptr<SkStreamAsset> stream) |
bungeman@google.com | 9df621d | 2011-06-23 21:43:52 +0000 | [diff] [blame] | 91 | : SkBaseIStream() |
Ben Wagner | 11eae3d | 2019-08-22 17:40:34 -0400 | [diff] [blame] | 92 | , fSkStream(std::move(stream)) |
bungeman@google.com | 6015792 | 2011-08-13 00:06:17 +0000 | [diff] [blame] | 93 | , fLocation() |
| 94 | { |
| 95 | this->fSkStream->rewind(); |
| 96 | } |
bungeman@google.com | 9df621d | 2011-06-23 21:43:52 +0000 | [diff] [blame] | 97 | |
Ben Wagner | 11eae3d | 2019-08-22 17:40:34 -0400 | [diff] [blame] | 98 | SkIStream::~SkIStream() {} |
bungeman@google.com | 9df621d | 2011-06-23 21:43:52 +0000 | [diff] [blame] | 99 | |
Ben Wagner | 11eae3d | 2019-08-22 17:40:34 -0400 | [diff] [blame] | 100 | HRESULT SkIStream::CreateFromSkStream(std::unique_ptr<SkStreamAsset> stream, IStream** ppStream) { |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 101 | if (nullptr == stream) { |
bungeman@google.com | 635091f | 2013-10-01 15:03:18 +0000 | [diff] [blame] | 102 | return E_INVALIDARG; |
| 103 | } |
Ben Wagner | 11eae3d | 2019-08-22 17:40:34 -0400 | [diff] [blame] | 104 | *ppStream = new SkIStream(std::move(stream)); |
bungeman@google.com | 9df621d | 2011-06-23 21:43:52 +0000 | [diff] [blame] | 105 | return S_OK; |
| 106 | } |
| 107 | |
| 108 | // ISequentialStream Interface |
Ben Wagner | 6cb6a07 | 2019-08-12 18:30:27 -0400 | [diff] [blame] | 109 | SK_STDMETHODIMP SkIStream::Read(void* pv, ULONG cb, ULONG* pcbRead) { |
bungeman@google.com | 4b18f57 | 2013-07-22 15:21:23 +0000 | [diff] [blame] | 110 | *pcbRead = static_cast<ULONG>(this->fSkStream->read(pv, cb)); |
bungeman@google.com | 6015792 | 2011-08-13 00:06:17 +0000 | [diff] [blame] | 111 | this->fLocation.QuadPart += *pcbRead; |
bungeman@google.com | 9df621d | 2011-06-23 21:43:52 +0000 | [diff] [blame] | 112 | return (*pcbRead == cb) ? S_OK : S_FALSE; |
| 113 | } |
| 114 | |
Ben Wagner | 6cb6a07 | 2019-08-12 18:30:27 -0400 | [diff] [blame] | 115 | SK_STDMETHODIMP SkIStream::Write(void const* pv, ULONG cb, ULONG* pcbWritten) { |
bungeman@google.com | 9df621d | 2011-06-23 21:43:52 +0000 | [diff] [blame] | 116 | return STG_E_CANTSAVE; |
| 117 | } |
| 118 | |
| 119 | // IStream Interface |
Ben Wagner | 6cb6a07 | 2019-08-12 18:30:27 -0400 | [diff] [blame] | 120 | SK_STDMETHODIMP SkIStream::Seek(LARGE_INTEGER liDistanceToMove, |
| 121 | DWORD dwOrigin, |
| 122 | ULARGE_INTEGER* lpNewFilePointer) |
bungeman@google.com | 9df621d | 2011-06-23 21:43:52 +0000 | [diff] [blame] | 123 | { |
bungeman@google.com | 9df621d | 2011-06-23 21:43:52 +0000 | [diff] [blame] | 124 | HRESULT hr = S_OK; |
bungeman@google.com | 6015792 | 2011-08-13 00:06:17 +0000 | [diff] [blame] | 125 | |
bungeman@google.com | 9df621d | 2011-06-23 21:43:52 +0000 | [diff] [blame] | 126 | switch(dwOrigin) { |
| 127 | case STREAM_SEEK_SET: { |
| 128 | if (!this->fSkStream->rewind()) { |
| 129 | hr = E_FAIL; |
| 130 | } else { |
Chris Dalton | 1ef8094 | 2017-12-04 12:01:30 -0700 | [diff] [blame] | 131 | size_t skip = static_cast<size_t>(liDistanceToMove.QuadPart); |
| 132 | size_t skipped = this->fSkStream->skip(skip); |
bungeman@google.com | 6015792 | 2011-08-13 00:06:17 +0000 | [diff] [blame] | 133 | this->fLocation.QuadPart = skipped; |
Chris Dalton | 1ef8094 | 2017-12-04 12:01:30 -0700 | [diff] [blame] | 134 | if (skipped != skip) { |
bungeman@google.com | 9df621d | 2011-06-23 21:43:52 +0000 | [diff] [blame] | 135 | hr = E_FAIL; |
| 136 | } |
| 137 | } |
| 138 | break; |
| 139 | } |
| 140 | case STREAM_SEEK_CUR: { |
Chris Dalton | 1ef8094 | 2017-12-04 12:01:30 -0700 | [diff] [blame] | 141 | size_t skip = static_cast<size_t>(liDistanceToMove.QuadPart); |
| 142 | size_t skipped = this->fSkStream->skip(skip); |
bungeman@google.com | 6015792 | 2011-08-13 00:06:17 +0000 | [diff] [blame] | 143 | this->fLocation.QuadPart += skipped; |
Chris Dalton | 1ef8094 | 2017-12-04 12:01:30 -0700 | [diff] [blame] | 144 | if (skipped != skip) { |
bungeman@google.com | 9df621d | 2011-06-23 21:43:52 +0000 | [diff] [blame] | 145 | hr = E_FAIL; |
| 146 | } |
| 147 | break; |
| 148 | } |
| 149 | case STREAM_SEEK_END: { |
| 150 | if (!this->fSkStream->rewind()) { |
| 151 | hr = E_FAIL; |
| 152 | } else { |
Chris Dalton | 1ef8094 | 2017-12-04 12:01:30 -0700 | [diff] [blame] | 153 | size_t skip = static_cast<size_t>(this->fSkStream->getLength() + |
| 154 | liDistanceToMove.QuadPart); |
| 155 | size_t skipped = this->fSkStream->skip(skip); |
bungeman@google.com | 6015792 | 2011-08-13 00:06:17 +0000 | [diff] [blame] | 156 | this->fLocation.QuadPart = skipped; |
| 157 | if (skipped != skip) { |
bungeman@google.com | 9df621d | 2011-06-23 21:43:52 +0000 | [diff] [blame] | 158 | hr = E_FAIL; |
| 159 | } |
| 160 | } |
| 161 | break; |
| 162 | } |
| 163 | default: |
| 164 | hr = STG_E_INVALIDFUNCTION; |
| 165 | break; |
| 166 | } |
rmistry@google.com | d6176b0 | 2012-08-23 18:14:13 +0000 | [diff] [blame] | 167 | |
bsalomon | 49f085d | 2014-09-05 13:34:00 -0700 | [diff] [blame] | 168 | if (lpNewFilePointer) { |
bungeman@google.com | e25c684 | 2011-08-17 14:53:54 +0000 | [diff] [blame] | 169 | lpNewFilePointer->QuadPart = this->fLocation.QuadPart; |
bungeman@google.com | 6015792 | 2011-08-13 00:06:17 +0000 | [diff] [blame] | 170 | } |
bungeman@google.com | 9df621d | 2011-06-23 21:43:52 +0000 | [diff] [blame] | 171 | return hr; |
| 172 | } |
| 173 | |
Ben Wagner | 6cb6a07 | 2019-08-12 18:30:27 -0400 | [diff] [blame] | 174 | SK_STDMETHODIMP SkIStream::Stat(STATSTG* pStatstg, DWORD grfStatFlag) { |
bsalomon@google.com | 9d12f5c | 2011-09-29 18:08:18 +0000 | [diff] [blame] | 175 | if (0 == (grfStatFlag & STATFLAG_NONAME)) { |
bungeman@google.com | 9df621d | 2011-06-23 21:43:52 +0000 | [diff] [blame] | 176 | return STG_E_INVALIDFLAG; |
| 177 | } |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 178 | pStatstg->pwcsName = nullptr; |
bungeman@google.com | 9df621d | 2011-06-23 21:43:52 +0000 | [diff] [blame] | 179 | pStatstg->cbSize.QuadPart = this->fSkStream->getLength(); |
| 180 | pStatstg->clsid = CLSID_NULL; |
| 181 | pStatstg->type = STGTY_STREAM; |
| 182 | pStatstg->grfMode = STGM_READ; |
| 183 | return S_OK; |
| 184 | } |
| 185 | |
| 186 | |
| 187 | /** |
| 188 | * SkIWStream |
| 189 | */ |
| 190 | SkWIStream::SkWIStream(SkWStream* stream) |
| 191 | : SkBaseIStream() |
| 192 | , fSkWStream(stream) |
| 193 | { } |
| 194 | |
| 195 | SkWIStream::~SkWIStream() { |
bsalomon | 49f085d | 2014-09-05 13:34:00 -0700 | [diff] [blame] | 196 | if (this->fSkWStream) { |
bungeman@google.com | 9df621d | 2011-06-23 21:43:52 +0000 | [diff] [blame] | 197 | this->fSkWStream->flush(); |
| 198 | } |
| 199 | } |
| 200 | |
Ben Wagner | 6cb6a07 | 2019-08-12 18:30:27 -0400 | [diff] [blame] | 201 | HRESULT SkWIStream::CreateFromSkWStream(SkWStream* stream, IStream ** ppStream) { |
bungeman@google.com | 9df621d | 2011-06-23 21:43:52 +0000 | [diff] [blame] | 202 | *ppStream = new SkWIStream(stream); |
| 203 | return S_OK; |
| 204 | } |
| 205 | |
| 206 | // ISequentialStream Interface |
Ben Wagner | 6cb6a07 | 2019-08-12 18:30:27 -0400 | [diff] [blame] | 207 | SK_STDMETHODIMP SkWIStream::Write(void const* pv, ULONG cb, ULONG* pcbWritten) { |
bungeman@google.com | 9df621d | 2011-06-23 21:43:52 +0000 | [diff] [blame] | 208 | HRESULT hr = S_OK; |
| 209 | bool wrote = this->fSkWStream->write(pv, cb); |
| 210 | if (wrote) { |
| 211 | *pcbWritten = cb; |
| 212 | } else { |
| 213 | *pcbWritten = 0; |
| 214 | hr = S_FALSE; |
| 215 | } |
| 216 | return hr; |
| 217 | } |
| 218 | |
| 219 | // IStream Interface |
Ben Wagner | 6cb6a07 | 2019-08-12 18:30:27 -0400 | [diff] [blame] | 220 | SK_STDMETHODIMP SkWIStream::Commit(DWORD) { |
bungeman@google.com | 14fc321 | 2011-08-01 20:41:53 +0000 | [diff] [blame] | 221 | this->fSkWStream->flush(); |
| 222 | return S_OK; |
| 223 | } |
| 224 | |
Ben Wagner | 6cb6a07 | 2019-08-12 18:30:27 -0400 | [diff] [blame] | 225 | SK_STDMETHODIMP SkWIStream::Stat(STATSTG* pStatstg, DWORD grfStatFlag) { |
bsalomon@google.com | 9d12f5c | 2011-09-29 18:08:18 +0000 | [diff] [blame] | 226 | if (0 == (grfStatFlag & STATFLAG_NONAME)) { |
bungeman@google.com | 9df621d | 2011-06-23 21:43:52 +0000 | [diff] [blame] | 227 | return STG_E_INVALIDFLAG; |
| 228 | } |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 229 | pStatstg->pwcsName = nullptr; |
bungeman@google.com | 9df621d | 2011-06-23 21:43:52 +0000 | [diff] [blame] | 230 | pStatstg->cbSize.QuadPart = 0; |
| 231 | pStatstg->clsid = CLSID_NULL; |
| 232 | pStatstg->type = STGTY_STREAM; |
| 233 | pStatstg->grfMode = STGM_WRITE; |
| 234 | return S_OK; |
| 235 | } |
Mike Klein | 8f11d4d | 2018-01-24 12:42:55 -0500 | [diff] [blame] | 236 | #endif//defined(SK_BUILD_FOR_WIN) |