2 Copyright 2014 Marten Richter
4 This file is part of VOMP.
6 VOMP is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2 of the License, or
9 (at your option) any later version.
11 VOMP is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with VOMP. If not, see <https://www.gnu.org/licenses/>.
23 #include "surfacewin.h"
25 #include "messagequeue.h"
26 #include "teletxt/txtfont.h"
28 #define OSD_BUFFER_WIDTH 1280
29 #define OSD_BUFFER_HEIGHT 720
31 #include "staticartwork.h"
33 #include "osdwinvector.h"
35 #define EXTERNALPICTURE(name, fname, fileextension) #name,
37 char *ext_pict_name[] {
42 #undef EXTERNALPICTURE
45 class WICPictDecoder : public OsdVector::PictureDecoder {
47 WICPictDecoder(OsdVector::PictureReader* treader, OsdWinVector *posd);
48 virtual ~WICPictDecoder();
50 unsigned char *decodePicture(LoadIndex index, unsigned char * buffer, unsigned int length, bool freemem);
52 bool getDecodedPicture(struct OsdVector::PictureInfo& pict_inf);
54 void freeReference(void * ref);
57 OsdVector::PictureInfo pictInf;
59 IWICImagingFactory *wicfactory;
63 WICPictDecoder::WICPictDecoder(OsdVector::PictureReader* treader, OsdWinVector *posd) : OsdVector::PictureDecoder(treader)
67 HRESULT hres = CoCreateInstance(CLSID_WICImagingFactory,
68 NULL,CLSCTX_INPROC_SERVER,
69 IID_IWICImagingFactory, (void**)&wicfactory);
74 WICPictDecoder::~WICPictDecoder()
76 wicfactory->Release();
79 unsigned char * WICPictDecoder::decodePicture(LoadIndex index, unsigned char * buffer, unsigned int length, bool freemem)
81 if (pictInfValid) return buffer; // does support only one image at a Time;
83 IWICStream * stream = NULL;
84 IWICBitmapDecoder *decoder = NULL;
85 IWICBitmapFrameDecode *source = NULL;
86 IWICFormatConverter *converter = NULL;
90 hres = wicfactory->CreateStream(&stream);
95 Log::getInstance()->log("WICDecoder", Log::DEBUG, "Could not create stream %d", hres);
98 hres = stream->InitializeFromMemory(buffer, length);
102 Log::getInstance()->log("WICDecoder", Log::DEBUG, "Could not initialize from memory %d", hres);
108 hres = wicfactory->CreateDecoderFromStream(stream, NULL, WICDecodeMetadataCacheOnLoad, &decoder);
112 Log::getInstance()->log("WICDecoder", Log::DEBUG, "Could not create decoder %d", hres);
117 hres = decoder->GetFrame(0, &source);
121 Log::getInstance()->log("WICDecoder", Log::DEBUG, "Could not get source %d", hres);
127 hres = wicfactory->CreateFormatConverter(&converter);
131 Log::getInstance()->log("WICDecoder", Log::DEBUG, "Could not get source %d", hres);
138 hres= converter->Initialize(source, GUID_WICPixelFormat32bppPBGRA,
139 WICBitmapDitherTypeNone, NULL, 0.f, WICBitmapPaletteTypeMedianCut);
143 Log::getInstance()->log("WICDecoder", Log::DEBUG, "Could not initialise converter %d", hres);
144 converter->Release();
151 ID2D1RenderTarget * rt=osd->LockOsdDrawing();
155 hres = rt->CreateBitmapFromWicBitmap(converter, NULL, &bitmap);
157 D2D1_SIZE_F size = bitmap->GetSize();
158 pictInf.reference = (void*)bitmap;
159 pictInf.width = size.width;
160 pictInf.height = size.height;
161 pictInf.image = bitmap;
162 pictInf.decoder = this;
163 pictInf.type = OsdVector::PictureInfo::D2DBitmap;
164 pictInf.lindex = index;
171 osd->UnlockOsdDrawing();
173 converter->Release();
178 if (pictInfValid) return NULL;
182 void WICPictDecoder::freeReference(void * ref)
184 ID2D1Bitmap* bitmap = (ID2D1Bitmap*)ref;
185 if (ref) bitmap->Release();
188 bool WICPictDecoder::getDecodedPicture(struct OsdVector::PictureInfo& pict_inf)
190 if (!pictInfValid) return false;
192 pictInfValid = false;
197 //This is stuff for rendering the OSD
199 OsdWinVector::OsdWinVector()
203 currentosd_render.surf9 = NULL;
204 currentosd_render.surf10 = NULL;
205 currentosd_render.query9 = NULL;
206 currentosd_render.query10 = NULL;
207 currentosd_render.surfdxgi = NULL;
208 currentosd_render.sharedhandle = NULL;
209 currentosd_render.rendtarget2D = NULL;
210 currentosd_backbuffer = currentosd_backbuffer;
211 aspect_correction = 1.;
212 is_direct_write_initted = false;
216 fontaspect_corr = 1.f;
220 ttchar_end.atlas = 0;
221 ttchar_end.position = 0;
224 const wchar_t *fontname = L"Arial";
225 cur_fontname = (wchar_t*)malloc(2*wcslen(fontname) + 2);
226 wcscpy(cur_fontname, fontname);
228 WICPictDecoder * wicdecode = new WICPictDecoder(&reader, this);
229 reader.addDecoder(wicdecode);
233 OsdWinVector::~OsdWinVector()
235 if (cur_fontname) free(cur_fontname);
240 if (dwritefac) dwritefac->Release();
241 if (!fontnames.size()) {
242 std::vector<char*>::iterator itty = fontnames.begin();
243 while (itty != fontnames.end()) {
249 /*if (!fontnames_keys.size()) {
250 vector<char*>::iterator itty = fontnames_keys.begin();
251 while (itty != fontnames_keys.end()) {
257 if (font) font->Release();
258 if (fontface) fontface->Release();
264 int OsdWinVector::init()
266 if (initted) return 0;
269 if (!createDirect3D9Objects()) return 0;
271 // then create the Device
273 if (D3D10CreateDevice1(NULL, D3D10_DRIVER_TYPE_HARDWARE, NULL, D3D10_CREATE_DEVICE_BGRA_SUPPORT, D3D10_FEATURE_LEVEL_9_1,
274 D3D10_1_SDK_VERSION, &d3ddevice10) != S_OK) {
275 Log::getInstance()->log("OSD", Log::WARN, "Could not create Direct3D10 device!");
279 if (d3ddevice10->QueryInterface(__uuidof(IDXGIDevice), (void **)&dxgidevice) != S_OK) {
280 Log::getInstance()->log("OSD", Log::WARN, "Could not query Dxgi device!");
284 if (D2D1CreateFactory(D2D1_FACTORY_TYPE_MULTI_THREADED, __uuidof(ID2D1Factory),(void**) &d2dfactory) != S_OK) {
285 Log::getInstance()->log("OSD", Log::WARN, "Could not create Direct2dfactory");
294 DXGI_SURFACE_DESC surf_desc;
295 surf_desc.Width = OSD_BUFFER_WIDTH;
296 surf_desc.Height = OSD_BUFFER_HEIGHT;
297 surf_desc.Format = DXGI_FORMAT_R8G8B8A8_UNORM;
298 surf_desc.SampleDesc.Count = 1;
299 surf_desc.SampleDesc.Quality = 0;
303 D2D1_RENDER_TARGET_PROPERTIES rt_prop;
304 rt_prop.dpiX = rt_prop.dpiY = 0;
305 rt_prop.pixelFormat = D2D1::PixelFormat(DXGI_FORMAT_UNKNOWN, D2D1_ALPHA_MODE_PREMULTIPLIED);
306 rt_prop.minLevel = D2D1_FEATURE_LEVEL_9;
307 rt_prop.type = D2D1_RENDER_TARGET_TYPE_HARDWARE;
308 rt_prop.usage = D2D1_RENDER_TARGET_USAGE_NONE;
310 for (int i = 0; i < 3; i++) {
311 SurfaceInfo surfinfo;
312 surfinfo.surf9 = NULL;
313 surfinfo.surf10 = NULL;
314 surfinfo.query9 = NULL;
315 surfinfo.query10 = NULL;
316 surfinfo.surfdxgi = NULL;
317 surfinfo.sharedhandle = NULL;
318 surfinfo.rendtarget2D = NULL;
321 hres = d3ddevice->CreateTexture(OSD_BUFFER_WIDTH, OSD_BUFFER_HEIGHT, 1, D3DUSAGE_RENDERTARGET, /*D3DFMT_A8R8G8B8*/D3DFMT_A8R8G8B8,
322 D3DPOOL_DEFAULT, &surfinfo.surf9, &surfinfo.sharedhandle);
324 Log::getInstance()->log("OSD", Log::WARN, "Could not create Texture for Direct3D9 !");
329 hres = d3ddevice->CreateQuery(D3DQUERYTYPE_EVENT, &surfinfo.query9);
331 Log::getInstance()->log("OSD", Log::WARN, "Could not create Query for Direct3D9 !");
336 DXGI_SHARED_RESOURCE dxgi_res;
337 dxgi_res.Handle = surfinfo.sharedhandle;
339 ID3D10Resource * tempres = NULL;
340 hres = d3ddevice10->OpenSharedResource(surfinfo.sharedhandle, __uuidof(ID3D10Resource), (void**)&tempres);
342 Log::getInstance()->log("OSD", Log::WARN, "Could not open resourcee for Direct3D10 !");
347 hres = tempres->QueryInterface(__uuidof(ID3D10Texture2D), (void**)&surfinfo.surf10);
349 Log::getInstance()->log("OSD", Log::WARN, "Could not query surface for Direct3D10 !");
356 hres = surfinfo.surf10->QueryInterface(__uuidof(IDXGISurface), (void**)&surfinfo.surfdxgi);
358 Log::getInstance()->log("OSD", Log::WARN, "Could not query surface for DXGI !");
363 hres = d2dfactory->CreateDxgiSurfaceRenderTarget(surfinfo.surfdxgi, &rt_prop, &surfinfo.rendtarget2D);
365 Log::getInstance()->log("OSD", Log::WARN, "Could not create render target!");
370 surfinfo.rendtarget2D->GetDpi(&dpix, &dpiy);
373 surfinfo.rendtarget2D->CreateSolidColorBrush(D2D1::ColorF(0xFFFFFFFF, 1.f), &ttbrush);
376 D3D10_QUERY_DESC qdesc;
377 qdesc.Query = D3D10_QUERY_EVENT;
380 hres = d3ddevice10->CreateQuery(&qdesc, &surfinfo.query10);
382 Log::getInstance()->log("OSD", Log::WARN, "Could not create Query for Direct2D Direct3D10 !");
386 allsurfs.push_back(surfinfo);
387 d3dtod2d.push(surfinfo);
389 aspect_correction = ((float)OSD_BUFFER_HEIGHT) / 576.f / (((float)OSD_BUFFER_WIDTH) / 720.f);
398 initted = true; // must set this here or create surface won't work
406 int OsdWinVector::shutdown()
408 if (!initted) return 0;
416 if (currentosd_render.surf9) {
417 currentosd_render.surf9 = NULL;
420 if (currentosd_backbuffer.surf9) {
421 currentosd_backbuffer.surf9 = NULL;
426 for (int i = 0; i < tt_atlas.size(); i++)
428 tt_atlas[i]->Release();
431 tt_font_chars.clear();
435 while (allsurfs.size()) {
436 SurfaceInfo surfinfo = allsurfs.front();
437 allsurfs.pop_front();
438 if (surfinfo.rendtarget2D) {
439 ID2D1RenderTarget *temp = surfinfo.rendtarget2D;
440 surfinfo.rendtarget2D = NULL;
444 if (surfinfo.surf9) {
445 LPDIRECT3DTEXTURE9 temp = surfinfo.surf9;
446 surfinfo.surf9 = NULL;
450 if (surfinfo.query9) {
451 IDirect3DQuery9* temp = surfinfo.query9;
452 surfinfo.query9 = NULL;
456 if (surfinfo.surf10) {
457 ID3D10Texture2D * temp = surfinfo.surf10;
458 surfinfo.surf10 = NULL;
462 if (surfinfo.surfdxgi) {
463 IDXGISurface *temp = surfinfo.surfdxgi;
464 surfinfo.surfdxgi = NULL;
467 if (surfinfo.query10) {
468 ID3D10Query* temp = surfinfo.query10;
469 surfinfo.query10 = NULL;
474 while (d3dtod2d.size()) d3dtod2d.pop();
475 while (d2dtod3d.size()) d2dtod3d.pop();
482 ID2D1Factory *temp = d2dfactory;
488 ID3D10Device *temp = d3ddevice10;
493 shutdownDirect3D9Objects();
500 void OsdWinVector::initPaths()
502 ID2D1PathGeometry *current = NULL;
503 ID2D1GeometrySink *currentsink = NULL;
505 d2dfactory->CreatePathGeometry(¤t);
506 current->Open(¤tsink);
507 currentsink->BeginFigure(D2D1::Point2F(0, 0), D2D1_FIGURE_BEGIN_HOLLOW);
509 currentsink->AddLine(D2D1::Point2F(1.f, 0));
511 currentsink->EndFigure(D2D1_FIGURE_END_OPEN);
513 std_paths[PIHorzLine] = current;
516 currentsink->Close();
517 currentsink->Release();
520 d2dfactory->CreatePathGeometry(¤t);
521 current->Open(¤tsink);
522 currentsink->BeginFigure(D2D1::Point2F(0, 0), D2D1_FIGURE_BEGIN_HOLLOW);
524 currentsink->AddLine(D2D1::Point2F(0, 1.f));
525 currentsink->EndFigure(D2D1_FIGURE_END_OPEN);
528 std_paths[PIVertLine] = current;
531 currentsink->Close();
532 currentsink->Release();
535 d2dfactory->CreatePathGeometry(¤t);
536 current->Open(¤tsink);
537 currentsink->BeginFigure(D2D1::Point2F(0, 0), D2D1_FIGURE_BEGIN_FILLED);
539 currentsink->AddLine(D2D1::Point2F(1, 0));
540 currentsink->AddLine(D2D1::Point2F(1, 1));
541 currentsink->AddLine(D2D1::Point2F(0, 1));
542 currentsink->EndFigure(D2D1_FIGURE_END_CLOSED);
544 std_paths[PIRectangle] = current;
547 currentsink->Close();
548 currentsink->Release();
551 d2dfactory->CreatePathGeometry(¤t);
552 current->Open(¤tsink);
553 currentsink->BeginFigure(D2D1::Point2F(0, 0.f), D2D1_FIGURE_BEGIN_FILLED);
555 currentsink->AddArc(D2D1::ArcSegment(
556 D2D1::Point2F(0, 0.f),
557 D2D1::SizeF(1.f, 1.f),
559 D2D1_SWEEP_DIRECTION_COUNTER_CLOCKWISE,
560 D2D1_ARC_SIZE_LARGE));
562 currentsink->EndFigure(D2D1_FIGURE_END_CLOSED);
566 std_paths[PIPoint] = current;
569 currentsink->Close();
570 currentsink->Release();
575 void OsdWinVector::destroyPaths()
577 std_paths[PIHorzLine]->Release();
578 std_paths[PIVertLine]->Release();
579 std_paths[PIRectangle]->Release();
580 std_paths[PIPoint]->Release();
586 int OsdWinVector::loadFont(bool newfont)
589 if (!is_direct_write_initted)
592 hres = DWriteCreateFactory(DWRITE_FACTORY_TYPE_SHARED, __uuidof(IDWriteFactory),
593 (IUnknown**)(&dwritefac));
595 Log::getInstance()->log("OSD", Log::WARN, "Could not create DirectWrite Factory!");
599 is_direct_write_initted = true;
602 if (fontnames.size())
607 IDWriteFontCollection * fontcol = NULL;
608 if (dwritefac->GetSystemFontCollection(&fontcol) != S_OK) {
609 Log::getInstance()->log("OSD", Log::WARN, "Could not get System Font Collection!");
612 UINT num_font = fontcol->GetFontFamilyCount();
613 for (UINT i = 0; i < num_font; i++) {
614 IDWriteFontFamily * family=NULL;
615 if (fontcol->GetFontFamily(i, &family)!=S_OK) continue;
616 IDWriteLocalizedStrings* fam_name=NULL;
617 if (family->GetFamilyNames(&fam_name) != S_OK) {
623 if (fam_name->FindLocaleName(L"en-us", &id, &exist) == S_OK) {
626 fam_name->GetStringLength(id, &strlen);
627 wchar_t * stringbuf = new wchar_t[strlen + 1];
629 fam_name->GetString(id, stringbuf, strlen+1);
631 int strlenutf8 = WideCharToMultiByte(CP_UTF8, 0, stringbuf, -1,
633 char *stringbufutf8 = new char[strlenutf8 + 1];
634 WideCharToMultiByte(CP_UTF8, 0, stringbuf, -1,
635 stringbufutf8, strlenutf8+1, 0, NULL);
638 if (wcscmp(cur_fontname, stringbuf) == 0) {
639 IDWriteFont* new_font;
640 if (family->GetFirstMatchingFont(DWRITE_FONT_WEIGHT_NORMAL,
641 DWRITE_FONT_STRETCH_NORMAL, DWRITE_FONT_STYLE_NORMAL, &new_font) == S_OK) {
642 IDWriteFontFace *newfontface = NULL;
643 new_font->CreateFontFace(&newfontface);
645 IDWriteFont* temp_font = font;
646 IDWriteFontFace* temp_fontface = fontface;
648 fontface = newfontface;
650 if (temp_font) temp_font->Release();
651 if (temp_fontface) temp_fontface->Release();
656 fontnames.push_back(stringbufutf8);
664 DWRITE_FONT_METRICS metrics;
665 font->GetMetrics(&metrics);
666 fontheight = ((float)( metrics.ascent + metrics.descent + metrics.lineGap))*13.f
667 / ((float)metrics.designUnitsPerEm) / 576.f * ((float)OSD_BUFFER_HEIGHT)/72.f*dpiy;
668 fontzero = ((float)(metrics.ascent))*13.f
669 / ((float)metrics.designUnitsPerEm) / 576.f * ((float)OSD_BUFFER_HEIGHT) / 72.f*dpiy;
671 fontaspect_corr = ((float)OSD_BUFFER_HEIGHT) / ((float)OSD_BUFFER_WIDTH) * 720.f / 576.f;
674 for (UINT32 i = 0; i<256; i++) {
675 DWRITE_GLYPH_METRICS glyph_metrics;
677 fontface->GetGlyphIndices(&i, 1, &glyph_index);
678 HRESULT hres=fontface->GetDesignGlyphMetrics(&glyph_index, 1, &glyph_metrics, FALSE);
681 byte_char_width[i] = ((float)glyph_metrics.advanceWidth)*13.f
682 / ((float)metrics.designUnitsPerEm) *fontaspect_corr / 72.f*dpix;
685 byte_char_width[i] = 0.f;
699 OsdWinVector::TTIndex OsdWinVector::loadTTchar(cTeletextChar c)
701 /* map<unsigned int, TTIndex> tt_font_chars;
702 vector<ID2D1Bitmap *> tt_atlas;
703 TTIndex ttchar_end;*/
706 unsigned int glyph_index = c.getGlyphIndex();
707 std::map<unsigned int, TTIndex>::iterator glypos = tt_font_chars.find(glyph_index);
708 if (glypos != tt_font_chars.end())
710 return (*glypos).second;
712 TTIndex our_index = ttchar_end;
714 unsigned int buffer[10];
715 unsigned int * charmap = GetFontChar(c, buffer);
716 if (!charmap) { //invalid char
717 our_index.atlas = our_index.position = 0;
723 // Now we have to determine, if the atlas is already created
724 if (our_index.atlas == tt_atlas.size()) {
725 //Now we have to create the atlas
726 ID2D1Bitmap * atlas = NULL;
730 D2D1_BITMAP_PROPERTIES bitmap_prop;
731 bitmap_prop.dpiX = dpix;
732 bitmap_prop.dpiY = dpiy;
733 bitmap_prop.pixelFormat.format = DXGI_FORMAT_A8_UNORM;
734 bitmap_prop.pixelFormat.alphaMode = D2D1_ALPHA_MODE_STRAIGHT;
736 HRESULT hres = currentosd_backbuffer.rendtarget2D->CreateBitmap(size, bitmap_prop, &atlas);
739 Log::getInstance()->log("OsdWinVector", Log::DEBUG, "CreateBitmap for atlas failed %d", hres);
741 our_index.atlas = our_index.position = 0;
744 tt_atlas.push_back(atlas);
748 unsigned char buffer_final[120];
749 for (int i = 0; i<10; i++) {
750 for (int j = 0; j < 12; j++) {
751 buffer_final[i*12+j] = ((charmap[i] >> (16-j)) & 1)? 0xFF: 0x00;
755 D2D1_RECT_U dst_rect = { (our_index.position % 10)*12,
756 (our_index.position /10) *10,
757 (our_index.position % 10+1)*12,
758 (our_index.position / 10+1)*10,
761 HRESULT hres=tt_atlas[our_index.atlas]->CopyFromMemory(&dst_rect, buffer_final, 12);
764 Log::getInstance()->log("OsdWinVector", Log::DEBUG, "CopyFromMemory for atlas failed %d", hres);
765 our_index.atlas = our_index.position = 0;
769 tt_font_chars[glyph_index] = our_index;
770 ttchar_end.position++;
771 if (ttchar_end.position == 120) {
772 ttchar_end.position = 0;
779 int OsdWinVector::getFontNames(const char *** names, const char *** names_keys)
781 *names = (const char**)&(fontnames[0]);
782 *names_keys = (const char**)&(fontnames/*_keys*/[0]);
783 return fontnames.size();
786 void OsdWinVector::setFont(const char * fontname) {
788 wchar_t * wfontname= new wchar_t[strlen(fontname)+1];
790 MultiByteToWideChar(CP_UTF8, 0, fontname, -1,
791 wfontname, strlen(fontname) + 1);
793 if (wcscmp(wfontname, cur_fontname)) {
795 if (cur_fontname) free(cur_fontname);
796 cur_fontname = (wchar_t*)malloc(2*wcslen(wfontname) + 2);
797 wcscpy(cur_fontname, wfontname);
809 void OsdWinVector::drawSetTrans(SurfaceCommands & sc)
811 D2D1::Matrix3x2F trans = D2D1::Matrix3x2F::Identity();
813 trans = trans*D2D1::Matrix3x2F::Scale(((float)OSD_BUFFER_WIDTH) / 720.f, ((float)OSD_BUFFER_HEIGHT) / 576.f);
814 trans=D2D1::Matrix3x2F::Translation(0.f + sc.x, sc.y)*trans;
816 currentosd_backbuffer.rendtarget2D->SetTransform(trans);
822 void OsdWinVector::executeDrawCommand(SVGCommand & command)
824 D2D1::Matrix3x2F save_matrix;
825 D2D1::Matrix3x2F save_matrix2;
828 switch (command.instr) {
830 currentosd_backbuffer.rendtarget2D->GetTransform(&save_matrix);
832 D2D1::Matrix3x2F matrix;
834 D2D1::Matrix3x2F::Scale(command.w, command.h)*D2D1::Matrix3x2F::Translation(command.x, command.y)*save_matrix;
835 currentosd_backbuffer.rendtarget2D->SetTransform(matrix);
836 currentosd_backbuffer.rendtarget2D->FillGeometry(std_paths[command.target.path_index],
837 (ID2D1Brush*)command.reference);
839 currentosd_backbuffer.rendtarget2D->SetTransform(save_matrix);
843 currentosd_backbuffer.rendtarget2D->GetTransform(&save_matrix);
844 ID2D1Bitmap * bitmap = (ID2D1Bitmap*)command.target.image;
846 D2D1_SIZE_F size=bitmap->GetSize();
848 D2D1::Matrix3x2F matrix;
851 if (command.reference) { //special behaviour for bw images they act as a mask on the current paint
853 D2D1::Matrix3x2F::Scale(1.f*aspect_correction, 1.f)
854 *D2D1::Matrix3x2F::Translation(command.x, command.y)*save_matrix;
858 float scalex = command.w / size.width;
859 float scaley = command.h / size.height;
860 float tx = command.x;
861 float ty = command.y;
862 if (command.corner == TopLeftLimited) {
863 if (scalex != 0.f && scaley != 0.f) {
864 float imageaspect = size.width / size.height;
865 float boxaspect = command.w / command.h / aspect_correction;
866 if (imageaspect > boxaspect) {
868 ty += (command.h - size.height * scalex / aspect_correction)*0.5f;
872 tx += (command.w - size.width * scaley*aspect_correction)*0.5f;
878 if (scalex == 0.f && scaley == 0.f) {
879 scalex = aspect_correction;
882 else if (scalex == 0.f) {
883 scalex = scaley*aspect_correction;
885 else if (scaley == 0.f) {
886 scaley = scalex / aspect_correction;
890 if (command.corner == BottomRight || command.corner == BottomLeft || command.corner == BottomMiddle)
892 ty -= size.height * scaley;
895 if (command.corner == BottomRight || command.corner == TopRight)
897 tx -= size.width * scalex;
900 if (command.corner == BottomMiddle || command.corner == TopMiddle)
902 tx -= size.width * scalex *0.5f;
906 D2D1::Matrix3x2F::Scale(scalex, scaley)*D2D1::Matrix3x2F::Translation(tx, ty)*save_matrix;
911 currentosd_backbuffer.rendtarget2D->SetTransform(matrix);
913 if (command.reference) {
914 currentosd_backbuffer.rendtarget2D->FillOpacityMask(bitmap, (ID2D1Brush*)command.reference, D2D1_OPACITY_MASK_CONTENT_GRAPHICS);
919 currentosd_backbuffer.rendtarget2D->DrawBitmap(bitmap);
925 currentosd_backbuffer.rendtarget2D->SetTransform(save_matrix);
930 currentosd_backbuffer.rendtarget2D->GetTransform(&save_matrix);
932 D2D1::Matrix3x2F matrix;
933 matrix = D2D1::Matrix3x2F::Scale(fontaspect_corr, 1.f)*
934 D2D1::Matrix3x2F::Translation(command.x, command.y + fontzero)*save_matrix;
935 currentosd_backbuffer.rendtarget2D->SetTransform(matrix);
936 UINT32 tchar = command.target.textchar;
938 DWRITE_GLYPH_RUN glyph_run;
939 glyph_run.fontEmSize = 13.f / (74.f / 96.f);
940 glyph_run.fontFace=fontface;
941 glyph_run.glyphCount=1;
942 glyph_run.glyphIndices = &glyphindex;
944 fontface->GetGlyphIndices(&tchar, 1, &glyphindex);
947 DWRITE_GLYPH_OFFSET zero2 = { 0.f, 0.f };
948 glyph_run.glyphAdvances=&zero;
949 glyph_run.glyphOffsets=&zero2;
950 glyph_run.isSideways=false;
951 glyph_run.bidiLevel=0;
952 D2D1_POINT_2F pos = { 0.f, 0.f };
954 currentosd_backbuffer.rendtarget2D->DrawGlyphRun(pos, &glyph_run,
955 (ID2D1Brush*)command.reference);
956 currentosd_backbuffer.rendtarget2D->SetTransform(save_matrix);
961 tchar.setInternal(command.target.ttchar);
962 currentosd_backbuffer.rendtarget2D->GetTransform(&save_matrix);
964 enumTeletextColor ttforegcolour = tchar.GetFGColor();
965 enumTeletextColor ttbackgcolour = tchar.GetBGColor();
966 if (tchar.GetBoxedOut()) {
967 ttforegcolour = ttcTransparent;
968 ttbackgcolour = ttcTransparent;
970 D2D1::Matrix3x2F matrix;
971 matrix = D2D1::Matrix3x2F::Scale(1.5f+0.05f, 2.f)
972 *D2D1::Matrix3x2F::Translation(command.x + command.w*11.85f*1.4f, command.y + command.h*19.75f)
975 currentosd_backbuffer.rendtarget2D->SetTransform(matrix);
978 Colour color = Surface::enumTeletextColorToCoulour(ttbackgcolour);
980 ttbrush->SetColor(D2D1::ColorF(color.rgba(), ((float)color.alpha) / 255.f));
982 cTeletextChar filled;
983 filled.setInternal(0x187f);
984 TTIndex glyph_index = loadTTchar(filled);
987 D2D1_RECT_F src_rect = { (glyph_index.position % 10)*12.f,
988 (glyph_index.position / 10) *10.f,
989 (glyph_index.position % 10 + 1)*12.f,
990 (glyph_index.position / 10 + 1)*10.f,
994 currentosd_backbuffer.rendtarget2D->FillOpacityMask(tt_atlas[glyph_index.atlas], ttbrush, D2D1_OPACITY_MASK_CONTENT_GRAPHICS,
998 color = Surface::enumTeletextColorToCoulour(ttforegcolour);
999 ttbrush->SetColor(D2D1::ColorF(color.rgba(), ((float)color.alpha) / 255.f));
1002 glyph_index = loadTTchar(tchar);
1005 D2D1_RECT_F src_rect = { (glyph_index.position % 10)*12.f,
1006 (glyph_index.position / 10) *10.f,
1007 (glyph_index.position % 10 + 1)*12.f,
1008 (glyph_index.position / 10 + 1)*10.f,
1012 currentosd_backbuffer.rendtarget2D->FillOpacityMask(tt_atlas[glyph_index.atlas], ttbrush, D2D1_OPACITY_MASK_CONTENT_GRAPHICS,
1017 currentosd_backbuffer.rendtarget2D->SetTransform(save_matrix);
1021 case DrawClipping: {
1022 currentosd_backbuffer.rendtarget2D->GetTransform(&save_matrix);
1023 currentosd_backbuffer.rendtarget2D->SetTransform(D2D1::IdentityMatrix());
1025 D2D1_RECT_F coords = { ((command.x + clip_shift_x)*((float)OSD_BUFFER_WIDTH) / 720.f),
1026 (( command.y + clip_shift_y )*((float)OSD_BUFFER_HEIGHT) / 576.f),
1027 ((command.x + clip_shift_x+command.w - 1.f)*((float)OSD_BUFFER_WIDTH) / 720.f),
1028 (command.y + clip_shift_y+(command.h - 1.f)*((float)OSD_BUFFER_HEIGHT) / 576.f) };
1029 if (command.w == 0.f && command.h == 0.f) {
1030 while (num_clips > 0) {
1032 currentosd_backbuffer.rendtarget2D->PopAxisAlignedClip();
1036 currentosd_backbuffer.rendtarget2D->PushAxisAlignedClip(coords, D2D1_ANTIALIAS_MODE_ALIASED);
1039 currentosd_backbuffer.rendtarget2D->SetTransform(save_matrix);
1044 float OsdWinVector::getFontHeight()
1048 float OsdWinVector::getCharWidth(wchar_t c)
1050 if (c<256) return byte_char_width[c];
1051 if (!font) return 13.f;
1052 DWRITE_FONT_METRICS metrics;
1053 font->GetMetrics(&metrics);
1056 DWRITE_GLYPH_METRICS glyph_metrics;
1059 fontface->GetGlyphIndices(&tchar, 1, &glyph_index);
1060 fontface->GetDesignGlyphMetrics(&glyph_index, 1, &glyph_metrics, FALSE);
1062 return ((float)glyph_metrics.advanceWidth)*13.f
1063 / ((float)metrics.designUnitsPerEm) *fontaspect_corr / 72.f*dpiy;
1068 void OsdWinVector::getScreenSize(int &width, int &height)
1070 width = OSD_BUFFER_WIDTH;
1071 height = OSD_BUFFER_HEIGHT;
1074 void OsdWinVector::getRealScreenSize(int &width, int &height)
1076 width = BACKBUFFER_WIDTH;
1077 height = BACKBUFFER_HEIGHT;
1080 bool OsdWinVector::screenShot(void *buffer, int width, int height, bool osd /*include osd*/)
1082 //screen->screenShot(fileName);
1086 LPDIRECT3DTEXTURE9 OsdWinVector::getNextOsdTexture()
1089 if (d2dtod3d.size())
1091 SurfaceInfo surfinfo = d2dtod3d.front();
1092 //Now we have to check, if the resource is ready, or busy somewhere else
1093 // if (surfinfo.query10->GetData(NULL, 0, D3D10_ASYNC_GETDATA_DONOTFLUSH) == S_OK) {
1094 //the surface is ready
1095 if (currentosd_render.surf9) {
1096 currentosd_render.query9->Issue(D3DISSUE_END); //mark the end of the 9 api access
1097 d3dtod2d.push(currentosd_render);
1099 currentosd_render = surfinfo;
1103 queuemutex.unlock();
1105 return currentosd_render.surf9;
1108 void OsdWinVector::updateOsd()
1112 if (d3dtod2d.size())
1114 currentosd_backbuffer = d3dtod2d.front();
1115 //Now we have to check, if the resource is ready, or busy somewhere else
1116 if (currentosd_backbuffer.query9->GetData(NULL, 0, 0) == S_OK) {
1119 //queuemutex.unlock();
1120 currentosd_backbuffer.query10->Begin();
1122 currentosd_backbuffer.rendtarget2D->BeginDraw();
1123 currentosd_backbuffer.rendtarget2D->Clear(D2D1::ColorF(0.f, 0.f, 0.f, 0.0f));
1124 currentosd_backbuffer.rendtarget2D->SetAntialiasMode(D2D1_ANTIALIAS_MODE_ALIASED);
1129 while (num_clips>0) {
1130 currentosd_backbuffer.rendtarget2D->PopAxisAlignedClip();
1134 currentosd_backbuffer.rendtarget2D->EndDraw();
1135 currentosd_backbuffer.rendtarget2D->Flush();
1136 d3ddevice10->Flush();
1137 currentosd_backbuffer.query10->End();
1139 //queuemutex.lock();
1141 d2dtod3d.push(currentosd_backbuffer);
1147 queuemutex.unlock();
1151 ID2D1RenderTarget * OsdWinVector::LockOsdDrawing()
1154 if (d3dtod2d.size())
1156 currentosd_backbuffer=d3dtod2d.front();
1157 return currentosd_backbuffer.rendtarget2D;
1159 queuemutex.unlock();
1162 while (loop < 100) {
1164 if (d3dtod2d.size())
1166 currentosd_backbuffer = d3dtod2d.front();
1167 return currentosd_backbuffer.rendtarget2D;
1169 queuemutex.unlock();
1175 currentosd_backbuffer.surf10 = NULL;
1176 currentosd_backbuffer.surf9 = NULL;
1180 void OsdWinVector::UnlockOsdDrawing()
1182 queuemutex.unlock();
1185 void OsdWinVector::lostDestroyObjects()
1190 void OsdWinVector::lostRecreateObjects()
1196 void OsdWinVector::getTextureCoordinates(FLOAT* x, FLOAT* y)
1205 void OsdWinVector::destroyImageRef(ImageIndex index)
1207 ID2D1Bitmap* bitmap = (ID2D1Bitmap*)index;
1208 if (bitmap) bitmap->Release();
1212 bool OsdWinVector::getStaticImageData(unsigned int static_id, UCHAR **userdata, ULONG *length)
1214 if (sa_MAX>static_id) {
1215 HRSRC hrc = FindResource(GetModuleHandle(NULL), ext_pict_name[static_id], RT_RCDATA);
1216 *length = SizeofResource(GetModuleHandle(NULL), hrc);
1217 *userdata = (UCHAR*) LockResource(LoadResource(GetModuleHandle(NULL), hrc));
1228 void OsdWinVector::createPicture(struct PictureInfo& pict_inf)
1231 Log::getInstance()->log("OsdWinVector", Log::DEBUG, "TVMedia Create Picture %d", pict_inf.type);
1232 if (pict_inf.type == PictureInfo::D2DBitmap) {
1233 bool static_image = true;
1234 if (pict_inf.lindex & 0xffffffff) static_image = false;
1236 Message* m = new Message();
1237 // We have a pictures! send a message to ourself, to switch to gui thread
1239 m->p_to = Message::CONTROL;
1240 m->data = pict_inf.reference;
1241 if (!static_image) {
1242 m->message = Message::NEW_PICTURE;
1243 m->tag = pict_inf.lindex;
1246 m->message = Message::NEW_PICTURE_STATIC;
1247 m->tag = pict_inf.lindex >> 32LL;
1249 MessageQueue::getInstance()->postMessage(m); // inform control about new picture
1252 pict_inf.decoder->freeReference(pict_inf.reference);
1258 ImageIndex OsdWinVector::createMonoBitmap(void *base, int width, int height)
1260 // First we have to convert it to 8 bit alpha, we could use WIC but actually writing this code is not hard to port from base class
1265 unsigned char *basechar = (unsigned char*) base;
1266 unsigned int bytesIn, bitsIn;
1267 int widthBytes = width / 8;
1269 unsigned char *alphamem = (unsigned char*)malloc(width*height);
1270 unsigned char *alphamemrun = alphamem;
1272 for (y = 0; y < height; y++) {
1273 for (x = 0; x < width; x++) {
1274 bytesIn = (y * widthBytes) + (int)(x / 8);
1276 (*alphamemrun) = ((basechar[bytesIn] >> (7 - bitsIn)) & 0x01) ? 0xFF : 0x00;
1281 size.height = height;
1283 D2D1_BITMAP_PROPERTIES bitmap_prop;
1284 bitmap_prop.dpiX = dpix;
1285 bitmap_prop.dpiY = dpiy;
1286 bitmap_prop.pixelFormat.format = DXGI_FORMAT_A8_UNORM;
1287 bitmap_prop.pixelFormat.alphaMode = D2D1_ALPHA_MODE_STRAIGHT;
1289 ID2D1Bitmap *bitmap = NULL;
1291 HRESULT hres = currentosd_backbuffer.rendtarget2D->CreateBitmap(size, alphamem, width, bitmap_prop, &bitmap);
1294 Log::getInstance()->log("OsdWinVector", Log::DEBUG, "CreateBitmap failed %d", hres);
1302 return (void*)bitmap;
1306 ImageIndex OsdWinVector::createImagePalette(int width, int height, const unsigned char *image_data, const unsigned int*palette_data)
1308 // First we have to convert it to 32 bit RGBA, we could use WIC but actually writing this code is not hard to port from base class
1313 UINT32 *mem = (UINT32*)malloc(width*height*sizeof(UINT32));
1314 UINT32 *memrun = mem;
1315 const unsigned char* image_run = image_data;
1317 for (y = 0; y < height; y++) {
1318 for (x = 0; x < width; x++) {
1320 (*memrun) = palette_data[(*image_run)];
1326 size.height = height;
1328 D2D1_BITMAP_PROPERTIES bitmap_prop;
1329 bitmap_prop.dpiX = dpix;
1330 bitmap_prop.dpiY = dpiy;
1331 bitmap_prop.pixelFormat.format = DXGI_FORMAT_B8G8R8A8_UNORM;
1332 bitmap_prop.pixelFormat.alphaMode = D2D1_ALPHA_MODE_PREMULTIPLIED;
1334 ID2D1Bitmap *bitmap = NULL;
1336 HRESULT hres = currentosd_backbuffer.rendtarget2D->CreateBitmap(size, mem, width*sizeof(UINT32), bitmap_prop, &bitmap);
1339 Log::getInstance()->log("OsdWinVector", Log::DEBUG, "CreateBitmap failed %d", hres);
1347 return (void*)bitmap;
1351 void OsdWinVector::destroyStyleRef(VectorHandle index)
1353 if (!initted) return;
1354 ID2D1Brush *brush = (ID2D1Brush*)(index);
1359 VectorHandle OsdWinVector::createStyleRef(const DrawStyle &c)
1364 case DrawStyle::Color: {
1365 ID2D1SolidColorBrush * newbrush=NULL;
1366 currentosd_backbuffer.rendtarget2D->CreateSolidColorBrush(
1367 D2D1::ColorF(c.rgba(),((float)c.alpha)/255.f),
1372 case DrawStyle::GradientLinear:
1373 case DrawStyle::GradientRadial: {
1374 D2D1_GRADIENT_STOP grad_stop[5];
1375 grad_stop[0].color = D2D1::ColorF(c.rgba());
1376 grad_stop[0].position = 0.0f;
1378 for (int i = 0; i < (c.num_colors - 1); i++) {
1379 grad_stop[i + 1].color = D2D1::ColorF(c.grad_col[i].rgba(), ((float)c.grad_col[i].alpha) / 255.f);
1380 grad_stop[i + 1].position = c.grad_pos[i];
1382 grad_stop[c.num_colors].color = D2D1::ColorF(c.grad_col[c.num_colors - 1].rgba(), ((float)c.grad_col[c.num_colors - 1].alpha) / 255.f);
1383 grad_stop[c.num_colors].position = 1.f;
1385 ID2D1GradientStopCollection *gradstopobj;
1387 currentosd_backbuffer.rendtarget2D->CreateGradientStopCollection(
1391 D2D1_EXTEND_MODE_CLAMP,
1395 if (c.ft == DrawStyle::GradientLinear) {
1396 ID2D1LinearGradientBrush * newbrush=NULL;
1397 currentosd_backbuffer.rendtarget2D->CreateLinearGradientBrush(
1398 D2D1::LinearGradientBrushProperties(
1399 D2D1::Point2F(c.x1, c.y1),
1400 D2D1::Point2F(c.x2, c.y2)),
1404 gradstopobj->Release();
1408 if (c.ft == DrawStyle::GradientRadial) {
1409 ID2D1RadialGradientBrush * newbrush = NULL;
1410 currentosd_backbuffer.rendtarget2D->CreateRadialGradientBrush(
1411 D2D1::RadialGradientBrushProperties(
1412 D2D1::Point2F(c.x1, c.y1),
1413 D2D1::Point2F(c.x2, c.y2),c.r,c.r),
1417 gradstopobj->Release();