blob: 93b483bf783563a54c4334f9459f4365582e7474 [file] [log] [blame]
bungeman@google.com9df621d2011-06-23 21:43:52 +00001/*
epoger@google.comec3ed6a2011-07-28 14:26:00 +00002 * 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.com9df621d2011-06-23 21:43:52 +00006 */
7
Mike Kleinc0bd9f92019-04-23 12:05:21 -05008#include "include/core/SkTypes.h"
Mike Klein8f11d4d2018-01-24 12:42:55 -05009#if defined(SK_BUILD_FOR_WIN)
epoger@google.comec3ed6a2011-07-28 14:26:00 +000010
Mike Kleinc0bd9f92019-04-23 12:05:21 -050011#include "include/core/SkStream.h"
12#include "src/utils/win/SkIStream.h"
bungeman@google.com9df621d2011-06-23 21:43:52 +000013
14/**
15 * SkBaseIStream
16 */
17SkBaseIStream::SkBaseIStream() : _refcount(1) { }
18SkBaseIStream::~SkBaseIStream() { }
19
Ben Wagner6cb6a072019-08-12 18:30:27 -040020SK_STDMETHODIMP SkBaseIStream::QueryInterface(REFIID iid, void ** ppvObject) {
halcanary96fcdcc2015-08-27 07:41:13 -070021 if (nullptr == ppvObject) {
bungeman@google.com9df621d2011-06-23 21:43:52 +000022 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 {
halcanary96fcdcc2015-08-27 07:41:13 -070032 *ppvObject = nullptr;
rmistry@google.comd6176b02012-08-23 18:14:13 +000033 return E_NOINTERFACE;
bungeman@google.com9df621d2011-06-23 21:43:52 +000034 }
35}
36
Ben Wagner6cb6a072019-08-12 18:30:27 -040037SK_STDMETHODIMP_(ULONG) SkBaseIStream::AddRef() {
bungeman@google.com9df621d2011-06-23 21:43:52 +000038 return (ULONG)InterlockedIncrement(&_refcount);
39}
40
Ben Wagner6cb6a072019-08-12 18:30:27 -040041SK_STDMETHODIMP_(ULONG) SkBaseIStream::Release() {
bungeman@google.com9df621d2011-06-23 21:43:52 +000042 ULONG res = (ULONG) InterlockedDecrement(&_refcount);
43 if (0 == res) {
44 delete this;
45 }
46 return res;
47}
48
49// ISequentialStream Interface
Ben Wagner6cb6a072019-08-12 18:30:27 -040050SK_STDMETHODIMP SkBaseIStream::Read(void* pv, ULONG cb, ULONG* pcbRead)
bungeman@google.com9df621d2011-06-23 21:43:52 +000051{ return E_NOTIMPL; }
52
Ben Wagner6cb6a072019-08-12 18:30:27 -040053SK_STDMETHODIMP SkBaseIStream::Write(void const* pv, ULONG cb, ULONG* pcbWritten)
bungeman@google.com9df621d2011-06-23 21:43:52 +000054{ return E_NOTIMPL; }
55
56// IStream Interface
Ben Wagner6cb6a072019-08-12 18:30:27 -040057SK_STDMETHODIMP SkBaseIStream::SetSize(ULARGE_INTEGER)
bungeman@google.com9df621d2011-06-23 21:43:52 +000058{ return E_NOTIMPL; }
59
Ben Wagner6cb6a072019-08-12 18:30:27 -040060SK_STDMETHODIMP SkBaseIStream::CopyTo(IStream*, ULARGE_INTEGER, ULARGE_INTEGER*, ULARGE_INTEGER*)
bungeman@google.com9df621d2011-06-23 21:43:52 +000061{ return E_NOTIMPL; }
62
Ben Wagner6cb6a072019-08-12 18:30:27 -040063SK_STDMETHODIMP SkBaseIStream::Commit(DWORD)
bungeman@google.com9df621d2011-06-23 21:43:52 +000064{ return E_NOTIMPL; }
65
Ben Wagner6cb6a072019-08-12 18:30:27 -040066SK_STDMETHODIMP SkBaseIStream::Revert()
bungeman@google.com9df621d2011-06-23 21:43:52 +000067{ return E_NOTIMPL; }
68
Ben Wagner6cb6a072019-08-12 18:30:27 -040069SK_STDMETHODIMP SkBaseIStream::LockRegion(ULARGE_INTEGER, ULARGE_INTEGER, DWORD)
bungeman@google.com9df621d2011-06-23 21:43:52 +000070{ return E_NOTIMPL; }
71
Ben Wagner6cb6a072019-08-12 18:30:27 -040072SK_STDMETHODIMP SkBaseIStream::UnlockRegion(ULARGE_INTEGER, ULARGE_INTEGER, DWORD)
bungeman@google.com9df621d2011-06-23 21:43:52 +000073{ return E_NOTIMPL; }
74
Ben Wagner6cb6a072019-08-12 18:30:27 -040075SK_STDMETHODIMP SkBaseIStream::Clone(IStream**)
bungeman@google.com9df621d2011-06-23 21:43:52 +000076{ return E_NOTIMPL; }
77
Ben Wagner6cb6a072019-08-12 18:30:27 -040078SK_STDMETHODIMP SkBaseIStream::Seek(LARGE_INTEGER liDistanceToMove,
79 DWORD dwOrigin,
80 ULARGE_INTEGER* lpNewFilePointer)
bungeman@google.com9df621d2011-06-23 21:43:52 +000081{ return E_NOTIMPL; }
82
Ben Wagner6cb6a072019-08-12 18:30:27 -040083SK_STDMETHODIMP SkBaseIStream::Stat(STATSTG* pStatstg, DWORD grfStatFlag)
bungeman@google.com9df621d2011-06-23 21:43:52 +000084{ return E_NOTIMPL; }
85
86
87/**
88 * SkIStream
89 */
Ben Wagner11eae3d2019-08-22 17:40:34 -040090SkIStream::SkIStream(std::unique_ptr<SkStreamAsset> stream)
bungeman@google.com9df621d2011-06-23 21:43:52 +000091 : SkBaseIStream()
Ben Wagner11eae3d2019-08-22 17:40:34 -040092 , fSkStream(std::move(stream))
bungeman@google.com60157922011-08-13 00:06:17 +000093 , fLocation()
94{
95 this->fSkStream->rewind();
96}
bungeman@google.com9df621d2011-06-23 21:43:52 +000097
Ben Wagner11eae3d2019-08-22 17:40:34 -040098SkIStream::~SkIStream() {}
bungeman@google.com9df621d2011-06-23 21:43:52 +000099
Ben Wagner11eae3d2019-08-22 17:40:34 -0400100HRESULT SkIStream::CreateFromSkStream(std::unique_ptr<SkStreamAsset> stream, IStream** ppStream) {
halcanary96fcdcc2015-08-27 07:41:13 -0700101 if (nullptr == stream) {
bungeman@google.com635091f2013-10-01 15:03:18 +0000102 return E_INVALIDARG;
103 }
Ben Wagner11eae3d2019-08-22 17:40:34 -0400104 *ppStream = new SkIStream(std::move(stream));
bungeman@google.com9df621d2011-06-23 21:43:52 +0000105 return S_OK;
106}
107
108// ISequentialStream Interface
Ben Wagner6cb6a072019-08-12 18:30:27 -0400109SK_STDMETHODIMP SkIStream::Read(void* pv, ULONG cb, ULONG* pcbRead) {
bungeman@google.com4b18f572013-07-22 15:21:23 +0000110 *pcbRead = static_cast<ULONG>(this->fSkStream->read(pv, cb));
bungeman@google.com60157922011-08-13 00:06:17 +0000111 this->fLocation.QuadPart += *pcbRead;
bungeman@google.com9df621d2011-06-23 21:43:52 +0000112 return (*pcbRead == cb) ? S_OK : S_FALSE;
113}
114
Ben Wagner6cb6a072019-08-12 18:30:27 -0400115SK_STDMETHODIMP SkIStream::Write(void const* pv, ULONG cb, ULONG* pcbWritten) {
bungeman@google.com9df621d2011-06-23 21:43:52 +0000116 return STG_E_CANTSAVE;
117}
118
119// IStream Interface
Ben Wagner6cb6a072019-08-12 18:30:27 -0400120SK_STDMETHODIMP SkIStream::Seek(LARGE_INTEGER liDistanceToMove,
121 DWORD dwOrigin,
122 ULARGE_INTEGER* lpNewFilePointer)
bungeman@google.com9df621d2011-06-23 21:43:52 +0000123{
bungeman@google.com9df621d2011-06-23 21:43:52 +0000124 HRESULT hr = S_OK;
bungeman@google.com60157922011-08-13 00:06:17 +0000125
bungeman@google.com9df621d2011-06-23 21:43:52 +0000126 switch(dwOrigin) {
127 case STREAM_SEEK_SET: {
128 if (!this->fSkStream->rewind()) {
129 hr = E_FAIL;
130 } else {
Chris Dalton1ef80942017-12-04 12:01:30 -0700131 size_t skip = static_cast<size_t>(liDistanceToMove.QuadPart);
132 size_t skipped = this->fSkStream->skip(skip);
bungeman@google.com60157922011-08-13 00:06:17 +0000133 this->fLocation.QuadPart = skipped;
Chris Dalton1ef80942017-12-04 12:01:30 -0700134 if (skipped != skip) {
bungeman@google.com9df621d2011-06-23 21:43:52 +0000135 hr = E_FAIL;
136 }
137 }
138 break;
139 }
140 case STREAM_SEEK_CUR: {
Chris Dalton1ef80942017-12-04 12:01:30 -0700141 size_t skip = static_cast<size_t>(liDistanceToMove.QuadPart);
142 size_t skipped = this->fSkStream->skip(skip);
bungeman@google.com60157922011-08-13 00:06:17 +0000143 this->fLocation.QuadPart += skipped;
Chris Dalton1ef80942017-12-04 12:01:30 -0700144 if (skipped != skip) {
bungeman@google.com9df621d2011-06-23 21:43:52 +0000145 hr = E_FAIL;
146 }
147 break;
148 }
149 case STREAM_SEEK_END: {
150 if (!this->fSkStream->rewind()) {
151 hr = E_FAIL;
152 } else {
Chris Dalton1ef80942017-12-04 12:01:30 -0700153 size_t skip = static_cast<size_t>(this->fSkStream->getLength() +
154 liDistanceToMove.QuadPart);
155 size_t skipped = this->fSkStream->skip(skip);
bungeman@google.com60157922011-08-13 00:06:17 +0000156 this->fLocation.QuadPart = skipped;
157 if (skipped != skip) {
bungeman@google.com9df621d2011-06-23 21:43:52 +0000158 hr = E_FAIL;
159 }
160 }
161 break;
162 }
163 default:
164 hr = STG_E_INVALIDFUNCTION;
165 break;
166 }
rmistry@google.comd6176b02012-08-23 18:14:13 +0000167
bsalomon49f085d2014-09-05 13:34:00 -0700168 if (lpNewFilePointer) {
bungeman@google.come25c6842011-08-17 14:53:54 +0000169 lpNewFilePointer->QuadPart = this->fLocation.QuadPart;
bungeman@google.com60157922011-08-13 00:06:17 +0000170 }
bungeman@google.com9df621d2011-06-23 21:43:52 +0000171 return hr;
172}
173
Ben Wagner6cb6a072019-08-12 18:30:27 -0400174SK_STDMETHODIMP SkIStream::Stat(STATSTG* pStatstg, DWORD grfStatFlag) {
bsalomon@google.com9d12f5c2011-09-29 18:08:18 +0000175 if (0 == (grfStatFlag & STATFLAG_NONAME)) {
bungeman@google.com9df621d2011-06-23 21:43:52 +0000176 return STG_E_INVALIDFLAG;
177 }
halcanary96fcdcc2015-08-27 07:41:13 -0700178 pStatstg->pwcsName = nullptr;
bungeman@google.com9df621d2011-06-23 21:43:52 +0000179 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 */
190SkWIStream::SkWIStream(SkWStream* stream)
191 : SkBaseIStream()
192 , fSkWStream(stream)
193{ }
194
195SkWIStream::~SkWIStream() {
bsalomon49f085d2014-09-05 13:34:00 -0700196 if (this->fSkWStream) {
bungeman@google.com9df621d2011-06-23 21:43:52 +0000197 this->fSkWStream->flush();
198 }
199}
200
Ben Wagner6cb6a072019-08-12 18:30:27 -0400201HRESULT SkWIStream::CreateFromSkWStream(SkWStream* stream, IStream ** ppStream) {
bungeman@google.com9df621d2011-06-23 21:43:52 +0000202 *ppStream = new SkWIStream(stream);
203 return S_OK;
204}
205
206// ISequentialStream Interface
Ben Wagner6cb6a072019-08-12 18:30:27 -0400207SK_STDMETHODIMP SkWIStream::Write(void const* pv, ULONG cb, ULONG* pcbWritten) {
bungeman@google.com9df621d2011-06-23 21:43:52 +0000208 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 Wagner6cb6a072019-08-12 18:30:27 -0400220SK_STDMETHODIMP SkWIStream::Commit(DWORD) {
bungeman@google.com14fc3212011-08-01 20:41:53 +0000221 this->fSkWStream->flush();
222 return S_OK;
223}
224
Ben Wagner6cb6a072019-08-12 18:30:27 -0400225SK_STDMETHODIMP SkWIStream::Stat(STATSTG* pStatstg, DWORD grfStatFlag) {
bsalomon@google.com9d12f5c2011-09-29 18:08:18 +0000226 if (0 == (grfStatFlag & STATFLAG_NONAME)) {
bungeman@google.com9df621d2011-06-23 21:43:52 +0000227 return STG_E_INVALIDFLAG;
228 }
halcanary96fcdcc2015-08-27 07:41:13 -0700229 pStatstg->pwcsName = nullptr;
bungeman@google.com9df621d2011-06-23 21:43:52 +0000230 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 Klein8f11d4d2018-01-24 12:42:55 -0500236#endif//defined(SK_BUILD_FOR_WIN)