• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 
11 #include "include/core/SkTypes.h"
12 
13 #include "include/core/SkStream.h"
14 #include "include/private/base/SkMutex.h"
15 #include "src/utils/win/SkObjBase.h"
16 #include "src/utils/win/SkTScopedComPtr.h"
17 
18 #include <dwrite.h>
19 
20 /**
21  *  An SkStream backed by an IDWriteFontFileStream.
22  *  This allows Skia code to read an IDWriteFontFileStream.
23  */
24 class SkDWriteFontFileStream : public SkStreamMemory {
25 public:
26     explicit SkDWriteFontFileStream(IDWriteFontFileStream* fontFileStream);
27     ~SkDWriteFontFileStream() override;
28 
29     size_t read(void* buffer, size_t size) override;
30     bool isAtEnd() const override;
31     bool rewind() override;
32     size_t getPosition() const override;
33     bool seek(size_t position) override;
34     bool move(long offset) override;
35     size_t getLength() const override;
36     const void* getMemoryBase() override;
37 
duplicate()38     std::unique_ptr<SkDWriteFontFileStream> duplicate() const {
39         return std::unique_ptr<SkDWriteFontFileStream>(this->onDuplicate());
40     }
fork()41     std::unique_ptr<SkDWriteFontFileStream> fork() const {
42         return std::unique_ptr<SkDWriteFontFileStream>(this->onFork());
43     }
44 
45 private:
46     SkDWriteFontFileStream* onDuplicate() const override;
47     SkDWriteFontFileStream* onFork() const override;
48 
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
62     SK_STDMETHODIMP QueryInterface(REFIID iid, void** ppvObject) override;
63     SK_STDMETHODIMP_(ULONG) AddRef() override;
64     SK_STDMETHODIMP_(ULONG) Release() override;
65 
66     // IDWriteFontFileStream methods
67     SK_STDMETHODIMP ReadFileFragment(
68         void const** fragmentStart,
69         UINT64 fileOffset,
70         UINT64 fragmentSize,
71         void** fragmentContext) override;
72 
73     SK_STDMETHODIMP_(void) ReleaseFileFragment(void* fragmentContext) override;
74     SK_STDMETHODIMP GetFileSize(UINT64* fileSize) override;
75     SK_STDMETHODIMP GetLastWriteTime(UINT64* lastWriteTime) override;
76 
77     static HRESULT Create(SkStreamAsset* stream,
78                           SkDWriteFontFileStreamWrapper** streamFontFileStream);
79 
80 private:
81     explicit SkDWriteFontFileStreamWrapper(SkStreamAsset* stream);
~SkDWriteFontFileStreamWrapper()82     virtual ~SkDWriteFontFileStreamWrapper() { }
83 
84     ULONG fRefCount;
85     std::unique_ptr<SkStreamAsset> fStream;
86     SkMutex fStreamMutex;
87 };
88 #endif
89