image_test.go 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  1. // Copyright 2024 The Ebitengine Authors
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. package buffered_test
  15. import (
  16. "image"
  17. "testing"
  18. "github.com/hajimehoshi/ebiten/v2/internal/atlas"
  19. "github.com/hajimehoshi/ebiten/v2/internal/buffered"
  20. "github.com/hajimehoshi/ebiten/v2/internal/graphics"
  21. "github.com/hajimehoshi/ebiten/v2/internal/graphicsdriver"
  22. "github.com/hajimehoshi/ebiten/v2/internal/restorable"
  23. t "github.com/hajimehoshi/ebiten/v2/internal/testing"
  24. "github.com/hajimehoshi/ebiten/v2/internal/ui"
  25. )
  26. func TestMain(m *testing.M) {
  27. t.MainWithRunLoop(m)
  28. }
  29. func TestUnsyncedPixels(t *testing.T) {
  30. dst := buffered.NewImage(16, 16, atlas.ImageTypeRegular)
  31. // Add an entry for dotsBuffer at (0, 0).
  32. dst.WritePixels([]byte{0xff, 0xff, 0xff, 0xff}, image.Rect(0, 0, 1, 1))
  33. // Merge the entry into the cached pixels.
  34. // The entry for dotsBuffer is now gone in the current implementation.
  35. ok, err := dst.ReadPixels(ui.Get().GraphicsDriverForTesting(), make([]byte, 4*16*16), image.Rect(0, 0, 16, 16))
  36. if err != nil {
  37. t.Fatal(err)
  38. }
  39. if !ok {
  40. t.Fatal("ReadPixels failed")
  41. }
  42. // Call WritePixels with the outside region of (0, 0).
  43. dst.WritePixels(make([]byte, 4*2*2), image.Rect(1, 1, 3, 3))
  44. // Flush unsynced pixel cache.
  45. src := buffered.NewImage(16, 16, atlas.ImageTypeRegular)
  46. vs := make([]float32, 4*graphics.VertexFloatCount)
  47. graphics.QuadVerticesFromDstAndSrc(vs, 0, 0, 16, 16, 0, 0, 16, 16, 1, 1, 1, 1)
  48. is := graphics.QuadIndices()
  49. dr := image.Rect(0, 0, 16, 16)
  50. sr := [graphics.ShaderSrcImageCount]image.Rectangle{image.Rect(0, 0, 16, 16)}
  51. dst.DrawTriangles([graphics.ShaderSrcImageCount]*buffered.Image{src}, vs, is, graphicsdriver.BlendSourceOver, dr, sr, atlas.NearestFilterShader, nil, graphicsdriver.FillRuleFillAll, restorable.HintNone)
  52. // Check the result is correct.
  53. var got [4]byte
  54. ok, err = dst.ReadPixels(ui.Get().GraphicsDriverForTesting(), got[:], image.Rect(0, 0, 1, 1))
  55. if err != nil {
  56. t.Fatal(err)
  57. }
  58. if !ok {
  59. t.Fatal("ReadPixels failed")
  60. }
  61. want := [4]byte{0xff, 0xff, 0xff, 0xff}
  62. if got != want {
  63. t.Errorf("got: %v, want: %v", got, want)
  64. }
  65. }