void aquireResources() { if (videoWidth == 0 || videoHeight == 0) { throw new VideoPlayerException("Cannot instantiate D3D surface with a width or height of 0 pixels"); } D3D.Format pixelFormat = makeFourCC('Y', 'V', '1', '2'); offscreen = D3D.Surface.CreateOffscreenPlain(device, videoWidth, videoHeight, pixelFormat, D3D.Pool.Default); screenShot = D3D.Surface.CreateOffscreenPlain(device, videoWidth, videoHeight, D3D.Format.A8R8G8B8, D3D.Pool.Default); backBuffer = device.GetBackBuffer(0, 0); FontDescription fontDescription = new FontDescription(); fontDescription.FaceName = "TimesNewRoman"; fontDescription.Height = 15; infoFont = new D3D.Font(device, fontDescription); fontDescription = new FontDescription(); fontDescription.FaceName = "Arial"; videoDestRect = getVideoDestRect(backBuffer); fontDescription.Height = videoDestRect.Height / 14; fontDescription.Quality = FontQuality.Antialiased; subtitleShadowOffset = fontDescription.Height / 18; subtitleBottomMargin = videoDestRect.Height / 12; subtitleFont = new D3D.Font(device, fontDescription); }
private void HandleCaptureRequest(Device device) { try { for (int i = 0; i < BUFFERS; i++) { bool tmp; if (this.issuedQueries[i] && this.queries[i].GetData(out tmp, false)) { this.issuedQueries[i] = false; try { var lockedRect = this.surfaces[i].LockRectangle(LockFlags.ReadOnly); this.surfaceDataPointer = lockedRect.DataPointer; this.currentSurface = i; this.surfaceLocked[i] = true; } catch (Exception ex) { DebugMessage(ex.ToString()); } } } if (previousRequestId != null || lastRequestId != null) { this.previousRequestId = null; int nextCapture = (curCapture == BUFFERS - 1) ? 0 : (curCapture + 1); var sourceTexture = copySurfaces[curCapture]; try { using (var backbuffer = device.GetBackBuffer(0, 0)) { device.StretchRectangle(backbuffer, sourceTexture, TextureFilter.None); } if (copyWait < BUFFERS - 1) { copyWait++; } else { var prevSourceTexture = copySurfaces[nextCapture]; var targetTexture = surfaces[nextCapture]; if (this.surfaceLocked[nextCapture]) { lock (this.surfaceLocks[nextCapture]) { this.surfaces[nextCapture].UnlockRectangle(); this.surfaceLocked[nextCapture] = false; } } try { device.GetRenderTargetData(prevSourceTexture, targetTexture); } catch (Exception ex) { DebugMessage(ex.ToString()); } this.queries[nextCapture].Issue(Issue.End); this.issuedQueries[nextCapture] = true; } } catch (Exception ex) { this.DebugMessage(ex.ToString()); } curCapture = nextCapture; } } catch (Exception e) { this.DebugMessage(e.ToString()); } }
private void HandleCaptureRequest(Device device) { try { bool tmp; if (this.queryIssued && this.query.GetData(out tmp, false)) { this.queryIssued = false; var lockedRect = this.surface.LockRectangle(LockFlags.ReadOnly); this.surfaceDataPointer = lockedRect.DataPointer; this.surfaceLocked = true; this.copyEvent.Set(); } using (var backbuffer = device.GetBackBuffer(0, 0)) { device.StretchRectangle(backbuffer, this.renderTarget, TextureFilter.None); } if (this.surfaceLocked) { lock (this.renderTargetLock) { if (this.surfaceLocked) { this.surface.UnlockRectangle(); this.surfaceLocked = false; } } } try { var cooplevel = device.TestCooperativeLevel(); if (cooplevel.Code == ResultCode.Success.Code) { device.GetRenderTargetData(this.renderTarget, this.surface); this.query.Issue(Issue.End); this.queryIssued = true; } else { this.DebugMessage(string.Format("DirectX Error: TestCooperativeLevel = {0}", cooplevel.Code)); } } catch (Exception ex) { this.DebugMessage(ex.ToString()); } } catch (Exception e) { this.DebugMessage(e.ToString()); } }
private void HandleCaptureRequest(Device device) { if (Request == null) { return; } try { bool tmp; if (queryIssued && query.GetData(out tmp, false)) { queryIssued = false; var lockedRect = surface.LockRectangle(LockFlags.ReadOnly); surfaceDataPointer = lockedRect.DataPointer; surfaceLocked = true; new Thread(HandleCaptureRequestThread).Start(); } using (var backbuffer = device.GetBackBuffer(0, 0)) { device.StretchRectangle(backbuffer, renderTarget, TextureFilter.None); } if (surfaceLocked) { lock (renderTargetLock) { if (!this.surfaceLocked) { return; } this.surface.UnlockRectangle(); this.surfaceLocked = false; } } try { device.GetRenderTargetData(renderTarget, surface); query.Issue(Issue.End); queryIssued = true; } catch (Exception ex) { DebugMessage(ex.ToString()); } } catch (Exception e) { DebugMessage(e.ToString()); } }
private void HandleCaptureRequest(Device device) { try { if (_killThread) { return; } for (var i = 0; i < BUFFERS; i++) { bool tmp; if (_issuedQueries[i] && _queries[i].GetData(out tmp, false)) { _issuedQueries[i] = false; var lockedRect = _surfaces[i].LockRectangle(LockFlags.ReadOnly); _surfaceDataPointer = lockedRect.DataPointer; _currentSurface = i; _surfaceLocked[i] = true; _copyReadySignal.Set(); } } if (_captureWaitHandle.WaitOne(0) || _copyWait < BUFFERS - 1) { var nextCapture = (_curCapture == BUFFERS - 1) ? 0 : (_curCapture + 1); try { using (var backbuffer = device.GetBackBuffer(0, 0)) { var sourceTexture = _copySurfaces[_curCapture]; device.StretchRectangle(backbuffer, sourceTexture, TextureFilter.None); } if (_copyWait < BUFFERS - 1) { _copyWait++; } else { var prevSourceTexture = _copySurfaces[nextCapture]; var targetTexture = _surfaces[nextCapture]; if (_surfaceLocked[nextCapture]) { lock (_surfaceLocks[nextCapture]) { _surfaces[nextCapture].UnlockRectangle(); _surfaceLocked[nextCapture] = false; } } try { device.GetRenderTargetData(prevSourceTexture, targetTexture); } catch (Exception ex) { DebugMessage(ex.ToString()); } _queries[nextCapture].Issue(Issue.End); _issuedQueries[nextCapture] = true; } } catch (Exception ex) { DebugMessage(ex.ToString()); } finally { _captureWaitHandle.Reset(); _curCapture = nextCapture; } } } catch (Exception e) { DebugMessage(e.ToString()); } }
private void DoCaptureRenderTarget(Device device, string hook) { try { if (isCapturing && signalEnd.WaitOne(0)) { ClearD3D9Data(); isCapturing = false; stopRequested = true; } if (!isCapturing) { return; } if (!bHasTextures) { using (Surface backbuffer = device.GetRenderTarget(0)) { copyData.format = (int)backbuffer.Description.Format; copyData.width = backbuffer.Description.Width; copyData.height = backbuffer.Description.Height; } DoD3DHooks(device); } if (!bHasTextures) { return; } long timeVal = DateTime.Now.Ticks; long frameTime = 1000000 / targetFps; // lock at 30 fps if (frameTime != 0) { for (var i = 0; i < NUM_BUFFERS; i++) { if (issuedQueries[i]) { bool tmp; if (queries[i].GetData(out tmp, false)) { issuedQueries[i] = false; Surface targetTexture = textures[i]; try { var lockedRect = targetTexture.LockRectangle(LockFlags.ReadOnly); pCopyData = lockedRect.DataPointer; curCPUTexture = i; lockedTextures[i] = true; hCopyEvent.Set(); } catch (Exception ex) { DebugMessage(ex.ToString()); } } } } long timeElapsed = timeVal - lastTime; if (timeElapsed >= frameTime) { lastTime += frameTime; if (timeElapsed > frameTime * 2) { lastTime = timeVal; } var nextCapture = (curCapture == NUM_BUFFERS - 1) ? 0 : (curCapture + 1); Surface sourceTexture = copyD3D9Textures[curCapture]; using (var backbuffer = device.GetBackBuffer(0, 0)) { device.StretchRectangle(backbuffer, sourceTexture, TextureFilter.None); } if (copyWait < (NUM_BUFFERS - 1)) { copyWait++; } else { Surface prevSourceTexture = copyD3D9Textures[nextCapture]; Surface targetTexture = textures[nextCapture]; if (lockedTextures[nextCapture]) { Monitor.Enter(dataMutexes[nextCapture]); targetTexture.UnlockRectangle(); lockedTextures[nextCapture] = false; Monitor.Exit(dataMutexes[nextCapture]); } try { device.GetRenderTargetData(prevSourceTexture, targetTexture); } catch (Exception ex) { DebugMessage(ex.ToString()); } queries[nextCapture].Issue(Issue.End); issuedQueries[nextCapture] = true; } curCapture = nextCapture; } } } catch (Exception e) { DebugMessage("Error in PresentHook: " + e); } }