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 | */ |
bungeman | 2dca817 | 2015-01-22 06:08:31 -0800 | [diff] [blame] | 90 | SkIStream::SkIStream(SkStream* stream, bool deleteOnRelease) |
bungeman@google.com | 9df621d | 2011-06-23 21:43:52 +0000 | [diff] [blame] | 91 | : SkBaseIStream() |
| 92 | , fSkStream(stream) |
bungeman | 2dca817 | 2015-01-22 06:08:31 -0800 | [diff] [blame] | 93 | , fDeleteOnRelease(deleteOnRelease) |
bungeman@google.com | 6015792 | 2011-08-13 00:06:17 +0000 | [diff] [blame] | 94 | , fLocation() |
| 95 | { |
| 96 | this->fSkStream->rewind(); |
| 97 | } |
bungeman@google.com | 9df621d | 2011-06-23 21:43:52 +0000 | [diff] [blame] | 98 | |
| 99 | SkIStream::~SkIStream() { |
bungeman | 44af7ba | 2015-01-22 07:33:51 -0800 | [diff] [blame] | 100 | if (fDeleteOnRelease) { |
bungeman | 2dca817 | 2015-01-22 06:08:31 -0800 | [diff] [blame] | 101 | delete this->fSkStream; |
bungeman@google.com | 9df621d | 2011-06-23 21:43:52 +0000 | [diff] [blame] | 102 | } |
| 103 | } |
| 104 | |
Ben Wagner | 6cb6a07 | 2019-08-12 18:30:27 -0400 | [diff] [blame^] | 105 | HRESULT SkIStream::CreateFromSkStream(SkStream* stream, bool deleteOnRelease, IStream ** ppStream) { |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 106 | if (nullptr == stream) { |
bungeman@google.com | 635091f | 2013-10-01 15:03:18 +0000 | [diff] [blame] | 107 | return E_INVALIDARG; |
| 108 | } |
bungeman | 2dca817 | 2015-01-22 06:08:31 -0800 | [diff] [blame] | 109 | *ppStream = new SkIStream(stream, deleteOnRelease); |
bungeman@google.com | 9df621d | 2011-06-23 21:43:52 +0000 | [diff] [blame] | 110 | return S_OK; |
| 111 | } |
| 112 | |
| 113 | // ISequentialStream Interface |
Ben Wagner | 6cb6a07 | 2019-08-12 18:30:27 -0400 | [diff] [blame^] | 114 | SK_STDMETHODIMP SkIStream::Read(void* pv, ULONG cb, ULONG* pcbRead) { |
bungeman@google.com | 4b18f57 | 2013-07-22 15:21:23 +0000 | [diff] [blame] | 115 | *pcbRead = static_cast<ULONG>(this->fSkStream->read(pv, cb)); |
bungeman@google.com | 6015792 | 2011-08-13 00:06:17 +0000 | [diff] [blame] | 116 | this->fLocation.QuadPart += *pcbRead; |
bungeman@google.com | 9df621d | 2011-06-23 21:43:52 +0000 | [diff] [blame] | 117 | return (*pcbRead == cb) ? S_OK : S_FALSE; |
| 118 | } |
| 119 | |
Ben Wagner | 6cb6a07 | 2019-08-12 18:30:27 -0400 | [diff] [blame^] | 120 | SK_STDMETHODIMP SkIStream::Write(void const* pv, ULONG cb, ULONG* pcbWritten) { |
bungeman@google.com | 9df621d | 2011-06-23 21:43:52 +0000 | [diff] [blame] | 121 | return STG_E_CANTSAVE; |
| 122 | } |
| 123 | |
| 124 | // IStream Interface |
Ben Wagner | 6cb6a07 | 2019-08-12 18:30:27 -0400 | [diff] [blame^] | 125 | SK_STDMETHODIMP SkIStream::Seek(LARGE_INTEGER liDistanceToMove, |
| 126 | DWORD dwOrigin, |
| 127 | ULARGE_INTEGER* lpNewFilePointer) |
bungeman@google.com | 9df621d | 2011-06-23 21:43:52 +0000 | [diff] [blame] | 128 | { |
bungeman@google.com | 9df621d | 2011-06-23 21:43:52 +0000 | [diff] [blame] | 129 | HRESULT hr = S_OK; |
bungeman@google.com | 6015792 | 2011-08-13 00:06:17 +0000 | [diff] [blame] | 130 | |
bungeman@google.com | 9df621d | 2011-06-23 21:43:52 +0000 | [diff] [blame] | 131 | switch(dwOrigin) { |
| 132 | case STREAM_SEEK_SET: { |
| 133 | if (!this->fSkStream->rewind()) { |
| 134 | hr = E_FAIL; |
| 135 | } else { |
Chris Dalton | 1ef8094 | 2017-12-04 12:01:30 -0700 | [diff] [blame] | 136 | size_t skip = static_cast<size_t>(liDistanceToMove.QuadPart); |
| 137 | size_t skipped = this->fSkStream->skip(skip); |
bungeman@google.com | 6015792 | 2011-08-13 00:06:17 +0000 | [diff] [blame] | 138 | this->fLocation.QuadPart = skipped; |
Chris Dalton | 1ef8094 | 2017-12-04 12:01:30 -0700 | [diff] [blame] | 139 | if (skipped != skip) { |
bungeman@google.com | 9df621d | 2011-06-23 21:43:52 +0000 | [diff] [blame] | 140 | hr = E_FAIL; |
| 141 | } |
| 142 | } |
| 143 | break; |
| 144 | } |
| 145 | case STREAM_SEEK_CUR: { |
Chris Dalton | 1ef8094 | 2017-12-04 12:01:30 -0700 | [diff] [blame] | 146 | size_t skip = static_cast<size_t>(liDistanceToMove.QuadPart); |
| 147 | size_t skipped = this->fSkStream->skip(skip); |
bungeman@google.com | 6015792 | 2011-08-13 00:06:17 +0000 | [diff] [blame] | 148 | this->fLocation.QuadPart += skipped; |
Chris Dalton | 1ef8094 | 2017-12-04 12:01:30 -0700 | [diff] [blame] | 149 | if (skipped != skip) { |
bungeman@google.com | 9df621d | 2011-06-23 21:43:52 +0000 | [diff] [blame] | 150 | hr = E_FAIL; |
| 151 | } |
| 152 | break; |
| 153 | } |
| 154 | case STREAM_SEEK_END: { |
| 155 | if (!this->fSkStream->rewind()) { |
| 156 | hr = E_FAIL; |
| 157 | } else { |
scroggo@google.com | b5571b3 | 2013-09-25 21:34:24 +0000 | [diff] [blame] | 158 | // FIXME: Should not depend on getLength. |
| 159 | // See https://code.google.com/p/skia/issues/detail?id=1570 |
Chris Dalton | 1ef8094 | 2017-12-04 12:01:30 -0700 | [diff] [blame] | 160 | size_t skip = static_cast<size_t>(this->fSkStream->getLength() + |
| 161 | liDistanceToMove.QuadPart); |
| 162 | size_t skipped = this->fSkStream->skip(skip); |
bungeman@google.com | 6015792 | 2011-08-13 00:06:17 +0000 | [diff] [blame] | 163 | this->fLocation.QuadPart = skipped; |
| 164 | if (skipped != skip) { |
bungeman@google.com | 9df621d | 2011-06-23 21:43:52 +0000 | [diff] [blame] | 165 | hr = E_FAIL; |
| 166 | } |
| 167 | } |
| 168 | break; |
| 169 | } |
| 170 | default: |
| 171 | hr = STG_E_INVALIDFUNCTION; |
| 172 | break; |
| 173 | } |
rmistry@google.com | d6176b0 | 2012-08-23 18:14:13 +0000 | [diff] [blame] | 174 | |
bsalomon | 49f085d | 2014-09-05 13:34:00 -0700 | [diff] [blame] | 175 | if (lpNewFilePointer) { |
bungeman@google.com | e25c684 | 2011-08-17 14:53:54 +0000 | [diff] [blame] | 176 | lpNewFilePointer->QuadPart = this->fLocation.QuadPart; |
bungeman@google.com | 6015792 | 2011-08-13 00:06:17 +0000 | [diff] [blame] | 177 | } |
bungeman@google.com | 9df621d | 2011-06-23 21:43:52 +0000 | [diff] [blame] | 178 | return hr; |
| 179 | } |
| 180 | |
Ben Wagner | 6cb6a07 | 2019-08-12 18:30:27 -0400 | [diff] [blame^] | 181 | SK_STDMETHODIMP SkIStream::Stat(STATSTG* pStatstg, DWORD grfStatFlag) { |
bsalomon@google.com | 9d12f5c | 2011-09-29 18:08:18 +0000 | [diff] [blame] | 182 | if (0 == (grfStatFlag & STATFLAG_NONAME)) { |
bungeman@google.com | 9df621d | 2011-06-23 21:43:52 +0000 | [diff] [blame] | 183 | return STG_E_INVALIDFLAG; |
| 184 | } |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 185 | pStatstg->pwcsName = nullptr; |
scroggo@google.com | b5571b3 | 2013-09-25 21:34:24 +0000 | [diff] [blame] | 186 | // FIXME: Should not depend on getLength |
| 187 | // See https://code.google.com/p/skia/issues/detail?id=1570 |
bungeman@google.com | 9df621d | 2011-06-23 21:43:52 +0000 | [diff] [blame] | 188 | pStatstg->cbSize.QuadPart = this->fSkStream->getLength(); |
| 189 | pStatstg->clsid = CLSID_NULL; |
| 190 | pStatstg->type = STGTY_STREAM; |
| 191 | pStatstg->grfMode = STGM_READ; |
| 192 | return S_OK; |
| 193 | } |
| 194 | |
| 195 | |
| 196 | /** |
| 197 | * SkIWStream |
| 198 | */ |
| 199 | SkWIStream::SkWIStream(SkWStream* stream) |
| 200 | : SkBaseIStream() |
| 201 | , fSkWStream(stream) |
| 202 | { } |
| 203 | |
| 204 | SkWIStream::~SkWIStream() { |
bsalomon | 49f085d | 2014-09-05 13:34:00 -0700 | [diff] [blame] | 205 | if (this->fSkWStream) { |
bungeman@google.com | 9df621d | 2011-06-23 21:43:52 +0000 | [diff] [blame] | 206 | this->fSkWStream->flush(); |
| 207 | } |
| 208 | } |
| 209 | |
Ben Wagner | 6cb6a07 | 2019-08-12 18:30:27 -0400 | [diff] [blame^] | 210 | HRESULT SkWIStream::CreateFromSkWStream(SkWStream* stream, IStream ** ppStream) { |
bungeman@google.com | 9df621d | 2011-06-23 21:43:52 +0000 | [diff] [blame] | 211 | *ppStream = new SkWIStream(stream); |
| 212 | return S_OK; |
| 213 | } |
| 214 | |
| 215 | // ISequentialStream Interface |
Ben Wagner | 6cb6a07 | 2019-08-12 18:30:27 -0400 | [diff] [blame^] | 216 | SK_STDMETHODIMP SkWIStream::Write(void const* pv, ULONG cb, ULONG* pcbWritten) { |
bungeman@google.com | 9df621d | 2011-06-23 21:43:52 +0000 | [diff] [blame] | 217 | HRESULT hr = S_OK; |
| 218 | bool wrote = this->fSkWStream->write(pv, cb); |
| 219 | if (wrote) { |
| 220 | *pcbWritten = cb; |
| 221 | } else { |
| 222 | *pcbWritten = 0; |
| 223 | hr = S_FALSE; |
| 224 | } |
| 225 | return hr; |
| 226 | } |
| 227 | |
| 228 | // IStream Interface |
Ben Wagner | 6cb6a07 | 2019-08-12 18:30:27 -0400 | [diff] [blame^] | 229 | SK_STDMETHODIMP SkWIStream::Commit(DWORD) { |
bungeman@google.com | 14fc321 | 2011-08-01 20:41:53 +0000 | [diff] [blame] | 230 | this->fSkWStream->flush(); |
| 231 | return S_OK; |
| 232 | } |
| 233 | |
Ben Wagner | 6cb6a07 | 2019-08-12 18:30:27 -0400 | [diff] [blame^] | 234 | SK_STDMETHODIMP SkWIStream::Stat(STATSTG* pStatstg, DWORD grfStatFlag) { |
bsalomon@google.com | 9d12f5c | 2011-09-29 18:08:18 +0000 | [diff] [blame] | 235 | if (0 == (grfStatFlag & STATFLAG_NONAME)) { |
bungeman@google.com | 9df621d | 2011-06-23 21:43:52 +0000 | [diff] [blame] | 236 | return STG_E_INVALIDFLAG; |
| 237 | } |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 238 | pStatstg->pwcsName = nullptr; |
bungeman@google.com | 9df621d | 2011-06-23 21:43:52 +0000 | [diff] [blame] | 239 | pStatstg->cbSize.QuadPart = 0; |
| 240 | pStatstg->clsid = CLSID_NULL; |
| 241 | pStatstg->type = STGTY_STREAM; |
| 242 | pStatstg->grfMode = STGM_WRITE; |
| 243 | return S_OK; |
| 244 | } |
Mike Klein | 8f11d4d | 2018-01-24 12:42:55 -0500 | [diff] [blame] | 245 | #endif//defined(SK_BUILD_FOR_WIN) |