bungeman@google.com | e8f0592 | 2012-08-16 16:13:40 +0000 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2012 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. |
| 6 | */ |
mtklein | 1ee7651 | 2015-11-02 10:20:27 -0800 | [diff] [blame] | 7 | #include "SkTypes.h" |
| 8 | #if defined(SK_BUILD_FOR_WIN32) |
bungeman@google.com | e8f0592 | 2012-08-16 16:13:40 +0000 | [diff] [blame] | 9 | |
| 10 | #include "SkTypes.h" |
| 11 | #include "SkDWriteFontFileStream.h" |
| 12 | #include "SkHRESULT.h" |
bungeman@google.com | 11c9a55 | 2013-06-03 17:10:35 +0000 | [diff] [blame] | 13 | #include "SkTemplates.h" |
bungeman@google.com | f5cc5b1 | 2013-07-12 18:22:49 +0000 | [diff] [blame] | 14 | #include "SkTFitsIn.h" |
bungeman@google.com | 6cab1a4 | 2013-05-29 13:43:31 +0000 | [diff] [blame] | 15 | #include "SkTScopedComPtr.h" |
bungeman@google.com | e8f0592 | 2012-08-16 16:13:40 +0000 | [diff] [blame] | 16 | |
| 17 | #include <dwrite.h> |
bungeman@google.com | e8f0592 | 2012-08-16 16:13:40 +0000 | [diff] [blame] | 18 | |
| 19 | /////////////////////////////////////////////////////////////////////////////// |
| 20 | // SkIDWriteFontFileStream |
| 21 | |
| 22 | SkDWriteFontFileStream::SkDWriteFontFileStream(IDWriteFontFileStream* fontFileStream) |
bungeman@google.com | 6cab1a4 | 2013-05-29 13:43:31 +0000 | [diff] [blame] | 23 | : fFontFileStream(SkRefComPtr(fontFileStream)) |
bungeman@google.com | e8f0592 | 2012-08-16 16:13:40 +0000 | [diff] [blame] | 24 | , fPos(0) |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 25 | , fLockedMemory(nullptr) |
| 26 | , fFragmentLock(nullptr) { |
bungeman@google.com | e8f0592 | 2012-08-16 16:13:40 +0000 | [diff] [blame] | 27 | } |
| 28 | |
| 29 | SkDWriteFontFileStream::~SkDWriteFontFileStream() { |
| 30 | if (fFragmentLock) { |
| 31 | fFontFileStream->ReleaseFileFragment(fFragmentLock); |
| 32 | } |
| 33 | } |
| 34 | |
bungeman@google.com | e8f0592 | 2012-08-16 16:13:40 +0000 | [diff] [blame] | 35 | size_t SkDWriteFontFileStream::read(void* buffer, size_t size) { |
| 36 | HRESULT hr = S_OK; |
| 37 | |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 38 | if (nullptr == buffer) { |
bungeman@google.com | 6cab1a4 | 2013-05-29 13:43:31 +0000 | [diff] [blame] | 39 | size_t fileSize = this->getLength(); |
| 40 | |
| 41 | if (fPos + size > fileSize) { |
| 42 | size_t skipped = fileSize - fPos; |
| 43 | fPos = fileSize; |
| 44 | return skipped; |
bungeman@google.com | e8f0592 | 2012-08-16 16:13:40 +0000 | [diff] [blame] | 45 | } else { |
bungeman@google.com | 6cab1a4 | 2013-05-29 13:43:31 +0000 | [diff] [blame] | 46 | fPos += size; |
| 47 | return size; |
bungeman@google.com | e8f0592 | 2012-08-16 16:13:40 +0000 | [diff] [blame] | 48 | } |
| 49 | } |
| 50 | |
| 51 | const void* start; |
| 52 | void* fragmentLock; |
| 53 | hr = fFontFileStream->ReadFileFragment(&start, fPos, size, &fragmentLock); |
| 54 | if (SUCCEEDED(hr)) { |
| 55 | memcpy(buffer, start, size); |
| 56 | fFontFileStream->ReleaseFileFragment(fragmentLock); |
| 57 | fPos += size; |
| 58 | return size; |
| 59 | } |
| 60 | |
| 61 | //The read may have failed because we asked for too much data. |
bungeman@google.com | 6cab1a4 | 2013-05-29 13:43:31 +0000 | [diff] [blame] | 62 | size_t fileSize = this->getLength(); |
| 63 | if (fPos + size <= fileSize) { |
| 64 | //This means we were within bounds, but failed for some other reason. |
| 65 | return 0; |
| 66 | } |
| 67 | |
| 68 | size_t read = fileSize - fPos; |
| 69 | hr = fFontFileStream->ReadFileFragment(&start, fPos, read, &fragmentLock); |
| 70 | if (SUCCEEDED(hr)) { |
| 71 | memcpy(buffer, start, read); |
| 72 | fFontFileStream->ReleaseFileFragment(fragmentLock); |
| 73 | fPos = fileSize; |
| 74 | return read; |
| 75 | } |
| 76 | |
| 77 | return 0; |
| 78 | } |
| 79 | |
| 80 | bool SkDWriteFontFileStream::isAtEnd() const { |
| 81 | return fPos == this->getLength(); |
| 82 | } |
| 83 | |
| 84 | bool SkDWriteFontFileStream::rewind() { |
| 85 | fPos = 0; |
| 86 | return true; |
| 87 | } |
| 88 | |
| 89 | SkDWriteFontFileStream* SkDWriteFontFileStream::duplicate() const { |
halcanary | 385fe4d | 2015-08-26 13:07:48 -0700 | [diff] [blame] | 90 | return new SkDWriteFontFileStream(fFontFileStream.get()); |
bungeman@google.com | 6cab1a4 | 2013-05-29 13:43:31 +0000 | [diff] [blame] | 91 | } |
| 92 | |
| 93 | size_t SkDWriteFontFileStream::getPosition() const { |
| 94 | return fPos; |
| 95 | } |
| 96 | |
| 97 | bool SkDWriteFontFileStream::seek(size_t position) { |
| 98 | size_t length = this->getLength(); |
| 99 | fPos = (position > length) ? length : position; |
| 100 | return true; |
| 101 | } |
| 102 | |
| 103 | bool SkDWriteFontFileStream::move(long offset) { |
| 104 | return seek(fPos + offset); |
| 105 | } |
| 106 | |
| 107 | SkDWriteFontFileStream* SkDWriteFontFileStream::fork() const { |
bungeman | 2dca817 | 2015-01-22 06:08:31 -0800 | [diff] [blame] | 108 | SkAutoTDelete<SkDWriteFontFileStream> that(this->duplicate()); |
bungeman@google.com | 6cab1a4 | 2013-05-29 13:43:31 +0000 | [diff] [blame] | 109 | that->seek(fPos); |
mtklein | 18300a3 | 2016-03-16 13:53:35 -0700 | [diff] [blame^] | 110 | return that.release(); |
bungeman@google.com | 6cab1a4 | 2013-05-29 13:43:31 +0000 | [diff] [blame] | 111 | } |
| 112 | |
| 113 | size_t SkDWriteFontFileStream::getLength() const { |
| 114 | HRESULT hr = S_OK; |
bungeman@google.com | e8f0592 | 2012-08-16 16:13:40 +0000 | [diff] [blame] | 115 | UINT64 realFileSize = 0; |
| 116 | hr = fFontFileStream->GetFileSize(&realFileSize); |
bungeman@google.com | 11c9a55 | 2013-06-03 17:10:35 +0000 | [diff] [blame] | 117 | if (!SkTFitsIn<size_t>(realFileSize)) { |
bungeman@google.com | e8f0592 | 2012-08-16 16:13:40 +0000 | [diff] [blame] | 118 | return 0; |
| 119 | } |
bungeman@google.com | 6cab1a4 | 2013-05-29 13:43:31 +0000 | [diff] [blame] | 120 | return static_cast<size_t>(realFileSize); |
bungeman@google.com | e8f0592 | 2012-08-16 16:13:40 +0000 | [diff] [blame] | 121 | } |
| 122 | |
bungeman@google.com | 6cab1a4 | 2013-05-29 13:43:31 +0000 | [diff] [blame] | 123 | const void* SkDWriteFontFileStream::getMemoryBase() { |
| 124 | if (fLockedMemory) { |
| 125 | return fLockedMemory; |
| 126 | } |
| 127 | |
| 128 | UINT64 fileSize; |
| 129 | HRNM(fFontFileStream->GetFileSize(&fileSize), "Could not get file size"); |
| 130 | HRNM(fFontFileStream->ReadFileFragment(&fLockedMemory, 0, fileSize, &fFragmentLock), |
| 131 | "Could not lock file fragment."); |
| 132 | return fLockedMemory; |
| 133 | } |
bungeman@google.com | e8f0592 | 2012-08-16 16:13:40 +0000 | [diff] [blame] | 134 | |
| 135 | /////////////////////////////////////////////////////////////////////////////// |
| 136 | // SkIDWriteFontFileStreamWrapper |
| 137 | |
bungeman | 0babd3c | 2015-02-18 11:01:05 -0800 | [diff] [blame] | 138 | HRESULT SkDWriteFontFileStreamWrapper::Create(SkStreamAsset* stream, |
| 139 | SkDWriteFontFileStreamWrapper** streamFontFileStream) |
| 140 | { |
bungeman@google.com | e8f0592 | 2012-08-16 16:13:40 +0000 | [diff] [blame] | 141 | *streamFontFileStream = new SkDWriteFontFileStreamWrapper(stream); |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 142 | if (nullptr == streamFontFileStream) { |
bungeman@google.com | e8f0592 | 2012-08-16 16:13:40 +0000 | [diff] [blame] | 143 | return E_OUTOFMEMORY; |
| 144 | } |
| 145 | return S_OK; |
| 146 | } |
| 147 | |
bungeman | 0babd3c | 2015-02-18 11:01:05 -0800 | [diff] [blame] | 148 | SkDWriteFontFileStreamWrapper::SkDWriteFontFileStreamWrapper(SkStreamAsset* stream) |
scroggo | a1193e4 | 2015-01-21 12:09:53 -0800 | [diff] [blame] | 149 | : fRefCount(1), fStream(stream) { |
bungeman@google.com | e8f0592 | 2012-08-16 16:13:40 +0000 | [diff] [blame] | 150 | } |
| 151 | |
| 152 | HRESULT STDMETHODCALLTYPE SkDWriteFontFileStreamWrapper::QueryInterface(REFIID iid, void** ppvObject) { |
| 153 | if (iid == IID_IUnknown || iid == __uuidof(IDWriteFontFileStream)) { |
| 154 | *ppvObject = this; |
| 155 | AddRef(); |
| 156 | return S_OK; |
| 157 | } else { |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 158 | *ppvObject = nullptr; |
bungeman@google.com | e8f0592 | 2012-08-16 16:13:40 +0000 | [diff] [blame] | 159 | return E_NOINTERFACE; |
| 160 | } |
| 161 | } |
| 162 | |
| 163 | ULONG STDMETHODCALLTYPE SkDWriteFontFileStreamWrapper::AddRef() { |
| 164 | return InterlockedIncrement(&fRefCount); |
| 165 | } |
| 166 | |
| 167 | ULONG STDMETHODCALLTYPE SkDWriteFontFileStreamWrapper::Release() { |
| 168 | ULONG newCount = InterlockedDecrement(&fRefCount); |
| 169 | if (0 == newCount) { |
| 170 | delete this; |
| 171 | } |
| 172 | return newCount; |
| 173 | } |
| 174 | |
| 175 | HRESULT STDMETHODCALLTYPE SkDWriteFontFileStreamWrapper::ReadFileFragment( |
| 176 | void const** fragmentStart, |
| 177 | UINT64 fileOffset, |
| 178 | UINT64 fragmentSize, |
| 179 | void** fragmentContext) |
| 180 | { |
| 181 | // The loader is responsible for doing a bounds check. |
| 182 | UINT64 fileSize; |
| 183 | this->GetFileSize(&fileSize); |
| 184 | if (fileOffset > fileSize || fragmentSize > fileSize - fileOffset) { |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 185 | *fragmentStart = nullptr; |
| 186 | *fragmentContext = nullptr; |
bungeman@google.com | e8f0592 | 2012-08-16 16:13:40 +0000 | [diff] [blame] | 187 | return E_FAIL; |
| 188 | } |
| 189 | |
bungeman@google.com | f5cc5b1 | 2013-07-12 18:22:49 +0000 | [diff] [blame] | 190 | if (!SkTFitsIn<size_t>(fileOffset + fragmentSize)) { |
bungeman@google.com | e8f0592 | 2012-08-16 16:13:40 +0000 | [diff] [blame] | 191 | return E_FAIL; |
| 192 | } |
| 193 | |
| 194 | const void* data = fStream->getMemoryBase(); |
bsalomon | 49f085d | 2014-09-05 13:34:00 -0700 | [diff] [blame] | 195 | if (data) { |
bungeman@google.com | e8f0592 | 2012-08-16 16:13:40 +0000 | [diff] [blame] | 196 | *fragmentStart = static_cast<BYTE const*>(data) + static_cast<size_t>(fileOffset); |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 197 | *fragmentContext = nullptr; |
bungeman@google.com | e8f0592 | 2012-08-16 16:13:40 +0000 | [diff] [blame] | 198 | |
| 199 | } else { |
bungeman | 0babd3c | 2015-02-18 11:01:05 -0800 | [diff] [blame] | 200 | // May be called from multiple threads. |
bungeman@google.com | e8f0592 | 2012-08-16 16:13:40 +0000 | [diff] [blame] | 201 | SkAutoMutexAcquire ama(fStreamMutex); |
| 202 | |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 203 | *fragmentStart = nullptr; |
| 204 | *fragmentContext = nullptr; |
bungeman@google.com | e8f0592 | 2012-08-16 16:13:40 +0000 | [diff] [blame] | 205 | |
bungeman | 0babd3c | 2015-02-18 11:01:05 -0800 | [diff] [blame] | 206 | if (!fStream->seek(static_cast<size_t>(fileOffset))) { |
bungeman@google.com | e8f0592 | 2012-08-16 16:13:40 +0000 | [diff] [blame] | 207 | return E_FAIL; |
| 208 | } |
bungeman@google.com | 6cab1a4 | 2013-05-29 13:43:31 +0000 | [diff] [blame] | 209 | SkAutoTMalloc<uint8_t> streamData(static_cast<size_t>(fragmentSize)); |
bungeman@google.com | e8f0592 | 2012-08-16 16:13:40 +0000 | [diff] [blame] | 210 | if (fStream->read(streamData.get(), static_cast<size_t>(fragmentSize)) != fragmentSize) { |
| 211 | return E_FAIL; |
| 212 | } |
| 213 | |
| 214 | *fragmentStart = streamData.get(); |
mtklein | 18300a3 | 2016-03-16 13:53:35 -0700 | [diff] [blame^] | 215 | *fragmentContext = streamData.release(); |
bungeman@google.com | e8f0592 | 2012-08-16 16:13:40 +0000 | [diff] [blame] | 216 | } |
| 217 | return S_OK; |
| 218 | } |
| 219 | |
| 220 | void STDMETHODCALLTYPE SkDWriteFontFileStreamWrapper::ReleaseFileFragment(void* fragmentContext) { |
bungeman@google.com | 6cab1a4 | 2013-05-29 13:43:31 +0000 | [diff] [blame] | 221 | sk_free(fragmentContext); |
bungeman@google.com | e8f0592 | 2012-08-16 16:13:40 +0000 | [diff] [blame] | 222 | } |
| 223 | |
| 224 | HRESULT STDMETHODCALLTYPE SkDWriteFontFileStreamWrapper::GetFileSize(UINT64* fileSize) { |
| 225 | *fileSize = fStream->getLength(); |
| 226 | return S_OK; |
| 227 | } |
| 228 | |
| 229 | HRESULT STDMETHODCALLTYPE SkDWriteFontFileStreamWrapper::GetLastWriteTime(UINT64* lastWriteTime) { |
| 230 | // The concept of last write time does not apply to this loader. |
| 231 | *lastWriteTime = 0; |
| 232 | return E_NOTIMPL; |
| 233 | } |
mtklein | 1ee7651 | 2015-11-02 10:20:27 -0800 | [diff] [blame] | 234 | |
| 235 | #endif//defined(SK_BUILD_FOR_WIN32) |