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 | */ |
| 7 | |
| 8 | #ifndef SkDWriteFontFileStream_DEFINED |
| 9 | #define SkDWriteFontFileStream_DEFINED |
| 10 | |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 11 | #include "include/core/SkTypes.h" |
bungeman@google.com | e8f0592 | 2012-08-16 16:13:40 +0000 | [diff] [blame] | 12 | |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 13 | #include "include/core/SkStream.h" |
| 14 | #include "include/private/SkMutex.h" |
Ben Wagner | 6cb6a07 | 2019-08-12 18:30:27 -0400 | [diff] [blame^] | 15 | #include "src/utils/win/SkObjBase.h" |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 16 | #include "src/utils/win/SkTScopedComPtr.h" |
bungeman@google.com | e8f0592 | 2012-08-16 16:13:40 +0000 | [diff] [blame] | 17 | |
| 18 | #include <dwrite.h> |
| 19 | |
| 20 | /** |
| 21 | * An SkStream backed by an IDWriteFontFileStream. |
| 22 | * This allows Skia code to read an IDWriteFontFileStream. |
| 23 | */ |
bungeman@google.com | 6cab1a4 | 2013-05-29 13:43:31 +0000 | [diff] [blame] | 24 | class SkDWriteFontFileStream : public SkStreamMemory { |
bungeman@google.com | e8f0592 | 2012-08-16 16:13:40 +0000 | [diff] [blame] | 25 | public: |
| 26 | explicit SkDWriteFontFileStream(IDWriteFontFileStream* fontFileStream); |
Chris Dalton | 1ef8094 | 2017-12-04 12:01:30 -0700 | [diff] [blame] | 27 | ~SkDWriteFontFileStream() override; |
rmistry@google.com | d6176b0 | 2012-08-23 18:14:13 +0000 | [diff] [blame] | 28 | |
mtklein | 36352bf | 2015-03-25 18:17:31 -0700 | [diff] [blame] | 29 | size_t read(void* buffer, size_t size) override; |
| 30 | bool isAtEnd() const override; |
| 31 | bool rewind() override; |
mtklein | 36352bf | 2015-03-25 18:17:31 -0700 | [diff] [blame] | 32 | size_t getPosition() const override; |
| 33 | bool seek(size_t position) override; |
| 34 | bool move(long offset) override; |
mtklein | 36352bf | 2015-03-25 18:17:31 -0700 | [diff] [blame] | 35 | size_t getLength() const override; |
| 36 | const void* getMemoryBase() override; |
bungeman@google.com | e8f0592 | 2012-08-16 16:13:40 +0000 | [diff] [blame] | 37 | |
Mike Reed | 98c5d92 | 2017-09-15 21:39:47 -0400 | [diff] [blame] | 38 | std::unique_ptr<SkDWriteFontFileStream> duplicate() const { |
| 39 | return std::unique_ptr<SkDWriteFontFileStream>(this->onDuplicate()); |
| 40 | } |
| 41 | std::unique_ptr<SkDWriteFontFileStream> fork() const { |
| 42 | return std::unique_ptr<SkDWriteFontFileStream>(this->onFork()); |
| 43 | } |
Mike Reed | 98c5d92 | 2017-09-15 21:39:47 -0400 | [diff] [blame] | 44 | |
bungeman@google.com | e8f0592 | 2012-08-16 16:13:40 +0000 | [diff] [blame] | 45 | private: |
Mike Reed | 98c5d92 | 2017-09-15 21:39:47 -0400 | [diff] [blame] | 46 | SkDWriteFontFileStream* onDuplicate() const override; |
| 47 | SkDWriteFontFileStream* onFork() const override; |
Mike Reed | 98c5d92 | 2017-09-15 21:39:47 -0400 | [diff] [blame] | 48 | |
bungeman@google.com | e8f0592 | 2012-08-16 16:13:40 +0000 | [diff] [blame] | 49 | SkTScopedComPtr<IDWriteFontFileStream> fFontFileStream; |
| 50 | size_t fPos; |
| 51 | const void* fLockedMemory; |
| 52 | void* fFragmentLock; |
| 53 | }; |
| 54 | |
| 55 | /** |
| 56 | * An IDWriteFontFileStream backed by an SkStream. |
| 57 | * This allows DirectWrite to read an SkStream. |
| 58 | */ |
| 59 | class SkDWriteFontFileStreamWrapper : public IDWriteFontFileStream { |
| 60 | public: |
| 61 | // IUnknown methods |
Ben Wagner | 6cb6a07 | 2019-08-12 18:30:27 -0400 | [diff] [blame^] | 62 | SK_STDMETHODIMP QueryInterface(REFIID iid, void** ppvObject) override; |
| 63 | SK_STDMETHODIMP_(ULONG) AddRef() override; |
| 64 | SK_STDMETHODIMP_(ULONG) Release() override; |
bungeman@google.com | e8f0592 | 2012-08-16 16:13:40 +0000 | [diff] [blame] | 65 | |
| 66 | // IDWriteFontFileStream methods |
Ben Wagner | 6cb6a07 | 2019-08-12 18:30:27 -0400 | [diff] [blame^] | 67 | SK_STDMETHODIMP ReadFileFragment( |
bungeman@google.com | e8f0592 | 2012-08-16 16:13:40 +0000 | [diff] [blame] | 68 | void const** fragmentStart, |
| 69 | UINT64 fileOffset, |
| 70 | UINT64 fragmentSize, |
Ben Wagner | 6cb6a07 | 2019-08-12 18:30:27 -0400 | [diff] [blame^] | 71 | void** fragmentContext) override; |
bungeman@google.com | e8f0592 | 2012-08-16 16:13:40 +0000 | [diff] [blame] | 72 | |
Ben Wagner | 6cb6a07 | 2019-08-12 18:30:27 -0400 | [diff] [blame^] | 73 | SK_STDMETHODIMP_(void) ReleaseFileFragment(void* fragmentContext) override; |
| 74 | SK_STDMETHODIMP GetFileSize(UINT64* fileSize) override; |
| 75 | SK_STDMETHODIMP GetLastWriteTime(UINT64* lastWriteTime) override; |
bungeman@google.com | e8f0592 | 2012-08-16 16:13:40 +0000 | [diff] [blame] | 76 | |
bungeman | 0babd3c | 2015-02-18 11:01:05 -0800 | [diff] [blame] | 77 | static HRESULT Create(SkStreamAsset* stream, |
| 78 | SkDWriteFontFileStreamWrapper** streamFontFileStream); |
bungeman@google.com | e8f0592 | 2012-08-16 16:13:40 +0000 | [diff] [blame] | 79 | |
| 80 | private: |
bungeman | 0babd3c | 2015-02-18 11:01:05 -0800 | [diff] [blame] | 81 | explicit SkDWriteFontFileStreamWrapper(SkStreamAsset* stream); |
bungeman | d7f846b | 2014-06-19 11:26:59 -0700 | [diff] [blame] | 82 | virtual ~SkDWriteFontFileStreamWrapper() { } |
bungeman@google.com | e8f0592 | 2012-08-16 16:13:40 +0000 | [diff] [blame] | 83 | |
| 84 | ULONG fRefCount; |
Ben Wagner | 145dbcd | 2016-11-03 14:40:50 -0400 | [diff] [blame] | 85 | std::unique_ptr<SkStreamAsset> fStream; |
bungeman@google.com | e8f0592 | 2012-08-16 16:13:40 +0000 | [diff] [blame] | 86 | SkMutex fStreamMutex; |
| 87 | }; |
| 88 | #endif |